-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
uniform-int (and likely other distributions) are not as uniform as possible #45
Comments
Thanks @bigfarts. I think there are a few considerations here.
Thoughts? |
If you'd like a draft PR for the suggested change in bullet 1, let me know! |
For what it's worth, I put together an example of how various primitives can be stacked on top of each other to provide a truly uniform implementation of This example also uses |
uniform-int currently functions by stretching the output range of the RNG into the desired range. However, this is not strictly correct as it depends on the output size of the RNG. For instance, with a small-scale example, if the RNG only outputs 16 bits and we're generating a number from 0 to 5:
We can run a simulation to see the effect of this as well:
and we get:
In order to get a truly uniform distribution, the generator needs to instead:
https://github.com/python/cpython/blob/8a0d9a6bb77a72cd8b9ece01b7c1163fff28029a/Lib/random.py#L235-L243 has an example of how it works.
The text was updated successfully, but these errors were encountered: