There are two mathematically-defensible approaches here. The easy one samples from uniformly-spaced floating-point numbers on the interval: mask a random int to the number of significand bits, or with 1.0, subtract 1.0.https://twitter.com/ArvidGerstmann/status/1036661069878620161 …
-
Show this thread
-
The other method, abstractly, generates a random real number in [0,1), then rounds it to the nearest floating-point number.
@Riastradh_ did a nice writeup based on some quick email notes a few years ago: https://mumble.net/~campbell/2014/04/28/uniform-random-float …5 replies 1 retweet 7 likesShow this thread -
Replying to @stephentyrone @Riastradh_
As you described it this could produce a value which rounds to 1.0, which is obviously incorrect...
2 replies 0 retweets 0 likes -
Replying to @RichFelker @Riastradh_
Obviously correct, since both Python and C++ do it =P This is actually a really interesting question. Ideally one rounds down when sampling a half-open interval, but rejection sampling also works.
1 reply 0 retweets 1 like -
Replying to @stephentyrone @Riastradh_
Doesn't rounding down over-represent 0.0?
1 reply 0 retweets 0 likes -
If your random real is in [0,1), rounding it down is rounding toward zero.
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.