Skip to content

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

Closed
wants to merge 1 commit into from
Closed

Conversation

bhickey
Copy link

@bhickey bhickey commented Aug 24, 2016

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.

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.
@alexcrichton
Copy link
Contributor

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?

@bluss
Copy link
Contributor

bluss commented Aug 25, 2016

Does xoroshiro128+ really have a problem? I thought the statistical tests it brags about would cover that http://xoroshiro.di.unimi.it/

@bhickey
Copy link
Author

bhickey commented Aug 25, 2016

@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+:

Beside passing BigCrush, this generator passes the PractRand test suite
up to (and included) 16TB, with the exception of binary rank tests,
which fail due to the lowest bit being an LFSR; all other bits pass all
tests. We suggest to use a sign test to extract a random Boolean value.

@vks
Copy link
Collaborator

vks commented Nov 14, 2017

I think this also applies to next_u32, it should use the high bits rather than the low bits.

@dhardy
Copy link
Member

dhardy commented Nov 14, 2017

The next_u32 impl is up to the RNG; in the refactor we decided to recommend little-endian order (i.e. low bits) but allow RNGs to do otherwise (the Isaac64 generator now uses both parts if next_u32 is called twice).

@vks
Copy link
Collaborator

vks commented Nov 14, 2017

the Isaac64 generator now uses both parts if next_u32 is called twice

Does this really have better performance than just throwing away one part?

@burdges
Copy link
Contributor

burdges commented Nov 14, 2017

I'd expect two adjacent next_u32 might be inlined together occasionally, no?

@dhardy
Copy link
Member

dhardy commented Nov 14, 2017

Does this really have better performance than just throwing away one part?

Yes: dhardy#45

@pitdicker
Copy link
Contributor

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.

@dhardy dhardy closed this Dec 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-question Participation: opinions wanted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants