|Issue 558:||Math.random yields <= 30 bits|
|5 people starred this issue and may be notified of changes.||Back to list|
Calling Math.random() * 4294967296 (= 2^32) always yields a number divisible by 4, indicating that there are really only 30 bits in the mantissa of Math.random() that are ever non-zero. It would be nice to have at least 32 bits worth of randomness so that generation of random 32-bit integers would require only a single call to Math.random(). This issue was identified by a GWT user at the URL below. While we can work around this issue in GWT if need be, it would be nice to have an improved source of randomness in Chrome/V8. A simple histgram check in Chrome, Safari, and Firefox indicates that the 4 LSBs of (Math.random() * 4294967296) are pretty evenly distributed in Safari and Firefox, but only take on the values 0, 4, 8, and 12 in Chrome. I'm running Chrome 184.108.40.206 on Mac OS X 10.5.8. GWT issue: https://code.google.com/p/google-web-toolkit/issues/detail?id=4372
Jan 22, 2010
The analysis is correct. There is currently only 30 bits of entrophy in Math.random(), so when multiplying by 2^32, the lower two bits will always be zero.
Labels: Type-FeatureRequest Priority-Low
Jan 31, 2010
I have an inkling of what may be going on here: The Smi (small integer) class which is what Math.random returns only holds 31 bits of information. It also happens to be signed while Math.random only returns positive numbers. So that's 30 bits only. Now, I'm still trying to figure out how that translates to the 2 LSBs always being 0s.
Feb 1, 2010
Well, we start with a 30-bit quantity, divide by 0x40000000 to obtain a float between 0 and 1, then multiply by 0x100000000 (=2^32), so we get the original 30 bits shifted left two places.
Apr 6, 2010
Changelist http://codereview.chromium.org/1599019 fixes this issue. It is under review.
Apr 7, 2010
Thanks for addressing this issue.
Apr 12, 2010
(No comment was entered for this change.)
|► Sign in to add a comment|