-
Notifications
You must be signed in to change notification settings - Fork 0
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
Unbiased random selection from a set #3
Comments
(I've copied your reply from the other issue.)
Perhaps update the README to specify this implementation detail, because I didn't look at the implementation, just the README. I think it's OK to keep the README simple, so people can follow it, however, someone might use it to implement it's own thing, thus I believe having at least some warnings about the subtleties of the implementation might save one from doing the wrong thing. |
I could expand on this in the README, but
The pseudocode uses unsigned 128-bit integers/ |
Fair enough, but I've missed that... (And perhaps many like me would just skim through that section, and jump straight to the pseudo-code.)
Yes, but |
The Simple Modular Method should mean that the skew is so small that it doesn't matter. NIST recommends at least 64 extra bits (so 9 bytes on the left of |
After making my comment about the Spectre(.app) issue, I've also looked at your algorithm again, and I've seen that it also suffers from the biased random sampling due to the
%
usage.For example:
In all cases, if the
*.Length
is not prime, (or coprime with 2^128 I believe), then there is a slight bias in the frequencies with which certain items are selected.For more details, and a potential solution, see for example the following article:
=> https://dotat.at/@/2022-04-20-really-divisionless.html
Alternatively, perhaps even better, use a cryptographic library that provides unbiased random sampling.
Alternatively (2), for example see how Rust implements unbiased sampling and perhaps adapt from there:
=> https://github.com/rust-random/rand/blob/7fe350c9bac6b11a84687a7e8e33e6fd8e0a8c01/src/distr/uniform_int.rs#L211-L258
The text was updated successfully, but these errors were encountered: