-
-
Notifications
You must be signed in to change notification settings - Fork 645
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
getBigRandom() is incorrect #221
Comments
@FGasper Mayby I just don't understand your example, but with those two lines you will always get a number less than 10, right? So why do you write about getting greater than 10? |
@Ruffio Sorry, that was a bad example before. I’ve updated it to be (hopefully) clearer. |
@FGasper do you have an idea of a 'correct' implementation of 'getBigRandom'? This is a little over my head... :-( |
What is getBigRandom’s exact purpose? It seems to exclude 0 as a return value; is that by design? A more-correct implementation would be to discard any random values that are greater than the maximum value we’d want to return. Maybe something like:
Sorry for the untested-ness … hopefully later I can flesh it out a bit more. |
@kjur is this bias of probability by intend or accidental? Should it be changed? |
3 similar comments
@kjur is this bias of probability by intend or accidental? Should it be changed? |
@kjur is this bias of probability by intend or accidental? Should it be changed? |
@kjur is this bias of probability by intend or accidental? Should it be changed? |
@kjur what do you think? |
this replaces the previously remainder-based limiting of the random number which caused bias toward small numbers and excluded zero altogether by simple filtering as proposed frequently in kjur#221 and because the performance in most cases is actually faster than in the present implementation; also, an adaptation of swiftlang/swift#39143 has been considered but it performed significantly slower for large integers;
The above will unfairly privilege low numbers at the expense of high numbers. The initial BigInteger object is equally likely to be any number with the same bit length as “limit”. The modulo operation, then, makes for at least two separate ways by which the result of the operation can be a low number, while leaving only one path for the highest.
Example:
r_int is equally likely to be any of the integers 0 to 9, inclusive; i.e., each integer has a 0.1 probability.
r_int2, however, has a .2 probability of being 0 and a .2 probability of being 1, while the probability of other possible r_int2 values is still 0.1.
The text was updated successfully, but these errors were encountered: