You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just stumbled across an issue with the ULong method.
According to the documentation, it generates a random ulong between ulong.MinValue and ulong.MaxValue.
Unfortunately the method crashes due to an arithmetic overflow if the min parameter is very large (relatively close to MaxValue).
LINQPad Example or Reproduction Steps
The following test case will fail and trigger the overflow:
[Fact]publicvoidoverflow_test(){varrandomizer=newRandomizer();ulongmax=ulong.MaxValue;ulongmin=max-10;// I could go up to -3000 on my machineulongresult=randomizer.ULong(min,max);result.Should().BeInRange(min,max);}
Expected Behavior
Expected would be a random ulong in the desired range instead of a crash.
Stacktrace:
Failed Bogus.Tests.RandomizerTest.overflow_test [5 ms]
Error Message:
System.OverflowException : Arithmetic operation resulted in an overflow.
Stack Trace:
at System.Convert.ToUInt64(Double value)
at Bogus.Randomizer.ULong(UInt64 min, UInt64 max)
Altough I am not an expert on this, I believe it happens because of floating-point rounding.
The Double() method generates a random value between 0 and 1.
When multiplied by (max - min) — in this case, 10 — it produces a small double with many decimal places
(like 6.5668499183687 for example).
Adding the very large min value (Double() * (max - min) + min) results in a value where the mantissa cannot accurately represent the least significant bits, causing it to round up and exceed ulong.MaxValue.
Known Workarounds
I think a very simple way of fixing this and avoiding the rounding error is to convert the randomized double to a ulong before adding min, just like this:
Bogus NuGet Package
v35.6.1
.NET Version
.NET 8
Visual Studio Version
No response
What operating system are you using?
MacOS
What locale are you using with Bogus?
en
Problem Description
Hi everbody! 😃
I just stumbled across an issue with the ULong method.
According to the documentation, it generates a random
ulong
betweenulong.MinValue
andulong.MaxValue
.Unfortunately the method crashes due to an arithmetic overflow if the
min
parameter is very large (relatively close to MaxValue).LINQPad Example or Reproduction Steps
The following test case will fail and trigger the overflow:
Expected Behavior
Expected would be a random ulong in the desired range instead of a crash.
Actual Behavior
Actual implementation:
Altough I am not an expert on this, I believe it happens because of floating-point rounding.
Double()
method generates a random value between 0 and 1.(max - min)
— in this case, 10 — it produces a small double with many decimal places(like 6.5668499183687 for example).
min
value (Double() * (max - min) + min
) results in a value where the mantissa cannot accurately represent the least significant bits, causing it to round up and exceedulong.MaxValue
.Known Workarounds
I think a very simple way of fixing this and avoiding the rounding error is to convert the randomized
double
to aulong
before addingmin
, just like this:This implementation also passes all test cases inside RandomizerTest.cs.
Could you help with a pull-request?
Yes
The text was updated successfully, but these errors were encountered: