-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Generated floats are not uniformly distributed #30
Comments
Sure, feel free to open a PR! |
I'm not too familar how to do this with |
I've been looking at this for a bit and did some testing on the matter. You can use this playground to dive into how I was testing: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e46aa8e1706df69ff68fe5805c3779dc The results are basically that for values between 1 and 127 (inclusive), the current method won't tell the difference between something like The change that @knutwalker has talked about shows that it won't tell the difference only for values 1 and 2. That is, So although I'm not sure exactly what change should be implemented to random, or if one is needed at all, but it appears to only happen in the case |
So although it does seem for 128 different numbers at the tail end of the distribution it blends together, from my testing it seems to have no noticeable effect on the distribution. I used the code below to test multiple times how the distribution ends up and I feel it is sufficient for a non-cryptographically secure random generator. If you have a PR to submit @knutwalker then I think that wouldn't harm anything, but just leaving this comment to say the issue isn't noticeable.
|
Hej 👋
The documentation does not say anything about the distribution of the values, though one would assume that primitive values ought to be uniformly distributed. This is not the case for f32 and f64 based on
nanorand-rs/nanorand/src/gen.rs
Line 97 in 5e4ea40
nanorand-rs/nanorand/src/gen.rs
Line 103 in 5e4ea40
Floats above a certain value lose prevision, so using, say,
u32::MAX
andu32::MAX - 42
as the numerator both yield1.0
, which leads to a bias of the generated floats towards 1.0. This playground illustrates it a bit more: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=465eb2dcf33851195dde9f3a6601ea22If you want to fix that, a solution would be to use the "safe" value provided by
<float>::MANTISSA_DIGITS
as the max instead of<int>::MAX
. I could ope a PR for that if you like.The text was updated successfully, but these errors were encountered: