3 ms·
A rough calculation, a float64 rng which might reasonably supply billions of rnds an hour for simulations, would have a fair risk of hitting '1.0' every million
by strainer 10y ago
A rough calculation, a float64 rng which might reasonably supply billions of rnds an hour for simulations, would have a fair risk of hitting '1.0' every million hours of use, which means its important for production quality code to not run that risk.
A prng called Alea works with floating point arithmetic to generate rnds securely confined to [0,1] using a 'subtract with carry' scheme. It involves this kind of arrangement:
state=state*something+carry
carry=floor(state)
return state-carry //this is 0<1
Its very similar to linear congruential generator where top bits are masked away, in this case they are 'floored' off and fed back into the state. Alea seems to generate high quality rands and works extremely quickly in javascript engines by avoiding integer math.
My rnd utility module uses a similar algorithm. There may be a note of caution whether floating point math is standardised enough to use for a seedable prng, but so far my machines and phone have agreed in tests.
http://strainer.github.io/Fdrandom.js/ http://strainer.github.io/Fdrandom.js/
- mark-r 10y agoMy argument was for the other direction, using [0,1) when you actually want [0,1]. I agree that if 1.0 would cause a problem in your code you should use a rng that avoids it. All of the floating point random number generators I'm familiar with deliver [0,1).
- strainer 10y agoInteresting, I assumed java and javascripts behaviour were common. I dont recall seeing this notation before either [0,1) maybe i will now %)
- mark-r 10y agoI usually use C++ or Python, so I had to look up the others. From Java: "Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0." From Javascript: "Return a random number between 0 (inclusive) and 1 (exclusive)" So yes, all 4 are consistent in not including 1.0 in the range of random real numbers. I'd call that common. For more on the notation see https://en.wikipedia.org/wiki/Interval_(mathematics)#Including_or_excluding_endpoints https://en.wikipedia.org/wiki/Interval_(mathematics)#Includi...
- strainer 10y agoApologies I wasn't following properly, and thanks for the interval math pointer.