-
-
Notifications
You must be signed in to change notification settings - Fork 935
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
faker.number.int() returns numbers divisible by 2^[1...21] for high maximums #3367
Comments
Thanks for the detailed testing. Have you tested this with faker v9 as well? |
No problem thanks for your detailed testing again. If you need the same behavior for v8 you can create a new Faker instance and pass the 53bit randomizer. Lines 155 to 163 in 98ff809
|
Thank you for your quick reply and advice. The quickfix for v8 was to use smaller range. We replaced As conceptual fix, we'll update to v9. |
Pre-Checks
Describe the bug
This investigation started by finding out that
faker.number.int() % 4
is always true, regardless of the seed, regardless of how many times I query the value.We had following code in our tests:
The issue happens only for pretty large numbers (see my investigation below). For example, if I call it with max: 10000, numbers are correctly divisible. I suspect that the issue is somewhere when MAX_SAFE_INT bounds are exceeded within number.int() calculation. There is multiplication of the random number, which (probably) loses the precision.
I know that this isn't how to use faker. I should have provided
min
andmax
values toint()
method instead, which is what am doing right now. However, this is something that should at least deserve warning in the documentation, so people know that for numbers higher than x, precision (and randomness) is being lost.Minimal reproduction code
Additional Context
I was playing with following script to see how big precision is being lost. You can move EXPONENT variable up or down and will visually see that remainders start to become random.
As an outcome, we know that unbound faker.numbner.int() always returns numbers fully divisble by 2, 4, 8, ...2^21.
Environment Info
Which module system do you use?
Used Package Manager
pnpm
The text was updated successfully, but these errors were encountered: