colgreen / Redzen

General purpose C# code library.
Other
94 stars 16 forks source link

RandomSourceBase.NextDoubleNonZero() can produce zero #5

Closed colgreen closed 4 years ago

colgreen commented 4 years ago

This:

return ((NextULongInner() >> 11) & 0x1f_ffff_ffff_fffe) * INCR_DOUBLE;

Should be:

return (((NextULongInner() >> 11) & 0x1f_ffff_ffff_fffe)+1) * INCR_DOUBLE;

Otherwise a zero will be returned when NextULongInner() returns zero, which is approximately every 2^64 calls (approx. 1.8*10^19) if the underlying PRNG is capable of generating zero, which all of the redzen PRNGs are. This makes the error an exceedingly rare event, but it is possible and thus a definite bug. E.g. if this method were called a billion times a second, then we can expect to see a zero approximately once every 31 years!

colgreen commented 4 years ago

Fixed in the 9.x maintenance branch in release 9.1.1.

A fix for the master branch is pending.

colgreen commented 4 years ago

Fixed in master branch, and v11.0.0 release.