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
Improve random-ness of Parrot_range_rand #580
Comments
This should be fixed as of r44467. Will close after testing further. |
Replying to whiteknight:
Is there any test we could write that would demonstrate the problem and the solution? Thanks. kid51 |
11150 byte attachment from aghast
|
I have written a chi-square test for this, but even though the tests now pass frequently (something that they did not do before), they still occasionally fail due to insufficient randomness. Suggestions? |
Proposed random-range.t test for t/dynoplibs |
Our rand48 is better than libc rand() but it will not pass statistical tests. We should probe for /dev/urandom, /dev/random, pcg random, arc4random and rand48 (no idea how it is called on windows) and use it in this order, with mt and/or rand() used as fallbacks. |
I think the consensus on windows is to use CryptAcquireContext (passing a |
Parrot_range_rand(min, max, ignored) returns a number between min and max, inclusive.
However, it does so by computing a floating point number, multiplying that be the integer difference, and adding it to the min.
The result of this, in C, is that truncating conversion introduces a bias away from max.
Consider a call
Parrot_range_rand(0, 10, 0)
- the result is min (0 in our case) plus (max-min) (10-0, or 10) times a random number in the range 0.0 ... 1.0.But because of truncating conversion, 10*(rand) will convert to 0 about 10% of the time (0.000 ... 0.099), 1 about 10% of the time (0.100 ... 0.199), ..., and 9 about 10% of the time (0.900 ... 0.999). About 0% of the time (there is a change of hitting 1.0, but floats have a ''lot'' of decimal places...) the result will be 10.
Originally http://trac.parrot.org/parrot/ticket/1479
The text was updated successfully, but these errors were encountered: