-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Least significant bit of rand() is always zero #16344
Comments
If you count from the right then the distribution of each bit is |
edit: See https://github.com/art4711/random-double/blob/master/rd.c for some fun |
Fiddling with Matlab I see that their |
@tkelman The blob you linked to, is that the actual algorithm used for Julia? Or is it as you described random in [1.0,2.0) and then subtracting one? Because in the latter case I (almost) see how the probability distribution comes up. The floating point representation of 2.0 is (let's do it in Float16 so it's less typing) Including the 'hidden bit" we are looking at a binary string of 11 letters, the first being Now subtract from it 1.0, which corresponds to
If we really want to put that last bit to use, we can generate the float in "65-bit float" and truncate. Which, now that I am thinking about it, is basically the same as what is written in the |
The page I linked to is just some guy's fun little piece of C code, I don't think that's a commonly used algorithm. We could certainly port it from C to Julia and see how it compares in performance and distribution properties though. Julia subtracts 1:
|
There are 4 options here:
|
Some thoughts from the point of view of a user:
On 13 May 2016 at 05:58, Simon Byrne notifications@github.com wrote:
Euclid taught me that without assumptions there is no proof. Therefore, in |
I'm not quite sure what you mean here: what's |
|
Ah, I missed that comment. This is a consequence of option 1 above (or option 2 in the case of matlab), and nothing intrinsic to random numbers. Basically the nth bit will certainly be zero if it falls in the interval [0,2^(1-n)), and will be zero with the probability of 1/2 otherwise. |
Is there more to do here? |
From my perspective: not really. The result is not a bug (it is by design for performance issues). I'll close. |
Should this always be the case? For uniformly distributed random number in [0,1) I would expect the least significant bits to have some skew. But is it right that the final bit is always zero?
Version info:
The text was updated successfully, but these errors were encountered: