-
-
Notifications
You must be signed in to change notification settings - Fork 469
Change the Rand trait for bool #113
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
Conversation
To generate a bool, generates a 32-bit value and test the sign. Some common pseudo-random number generators exhibit an uneven quality distribution across their bits. Linear congruential generators return odd and even numbers in an alternating fashion. The low order bit of Xoroshiro128+ behaves like a linear feedback shift register. By testing the sign, we can instead rely on the high ordered bit of an emission, which will generally be of higher quality.
Isn't this still just testing one bit of the underlying RNG? Instead of the least significant bit it's just testing the most significant bit? Do RNGs tend to have uniform bit distributions by that point? |
Does xoroshiro128+ really have a problem? I thought the statistical tests it brags about would cover that http://xoroshiro.di.unimi.it/ |
@alexcrichton, M.E. O'Neill observed that the behavior of the high bit for LCGs follow a better distribution than the low bits. This forms the kernel of PCG. For several classes of fast RNGs (ex. MCG, LCG, Xorshift), this appears to hold true. It's easy, if not time consuming, to run BigCrush for each bit for a generator (i.e. grab the nth bit of a rng output 32 times). As an alternative to using the sign bit, we could take the parity. @bluss, from the author comments on Xoroshiro128+:
|
I think this also applies to |
The |
Does this really have better performance than just throwing away one part? |
I'd expect two adjacent |
Yes: dhardy#45 |
This was covered in dhardy#68. Sorry for doing double work, I did not look at the open PR's... For small non-cryptographic RNG's it is natural for the most significant bits to be of higher quality than the least-significant ones, because addition and multiplication always only affect the bits to the left. |
To generate a bool, generate a 32-bit value and test the sign.
Some common pseudo-random number generators exhibit an uneven quality distribution across their bits. Linear congruential generators return odd and even numbers in an alternating fashion. The low order bit of Xoroshiro128+ behaves like a linear feedback shift register. By testing the sign, we can instead rely on the high ordered bit of an emission, which will generally be of higher quality.