3 ms·
> I copied all of the examples I saw, and every one of them multiplied n by a randomized floating point number in the interval of (0,1] and take the whole numbe
by thaumasiotes 4y ago
> I copied all of the examples I saw, and every one of them multiplied n by a randomized floating point number in the interval of (0,1] and take the whole number part.
Really? I would have expected at least a 20:1 ratio in favor of [0,1) over (0,1].
Most obviously, (0,1] can't produce a uniform distribution, because the input value 1 produces its own unique output value. Whereas the input value 0 shares its output value with the rest of the interval (0, 1/n).
- hinkley 4y agoYeah I knew I was going to get the syntax wrong. My point is you're multiplying a float, not dividing an integer.
- thaumasiotes 4y agoI'm not really familiar with the larger issue, but the scaling method is easy as long as you have an easy way of producing a uniformly distributed random value between 0 and 1. That's not so easy to do; I suspect the fancier methods of generating random numbers are of interest because they have an easier requirement such as "32 random bits".
- hinkley 4y agoI've not looked at implementation details for long enough that I don't trust my answer, but I presume it has something to do with getting 30-some bits of data from the RNG, then coercing it to a floating point with zero on the left side of the mantissa.
- Dylan16807 4y ago> I'm not really familiar with the larger issue, but the scaling method is easy as long as you have an easy way of producing a uniformly distributed random value between 0 and 1. That's not so easy to do It's plenty easy to do. Start with 1.0 then fill the mantissa with random bits. For a flat distribution subtract 1.0. For an "every float is possible" distribution count the leading 0 bits in another chunk of random, and subtract count+1 from the exponent. You'd probably want the former for a situation like this.
- CodesInChaos 4y agoIn Germany we write `[0,1[` which is easier to remember.