top | item 24467978

(no title)

lsc36 | 5 years ago

1. You don't turn PRNG into "true" RNGs simply by picking seeds from environmental randomness. The seed is just the initial state, as long as the output is generated by a deterministic algorithm, by definition it's a PRNG. At the very best you can make a CSPRNG, but not a "true" RNG.

2. The dice roll example is not uniform distribution, I think this is a common pitfall when generating random integers of a range. `randomNumber % 6` results in a slight bias towards 0 and 1, since 2^31 % 6 == 2, there are more numbers in the range [0, 2^31-1] that map to 0 and 1 than those that map to 2...5. To make it uniform, for example, you should always discard if `randomNumber < 2` and regenerate another number for use.

discuss

order

blauditore|5 years ago

Re 2.: OTOH, the bias is extremely tiny, there's only 2 out of 2^31 cases which are problematic, or one in a billion. A generic RNG obviously shouldn't do it this way, but for most applications it would be good-enough really.

lsc36|5 years ago

The article claims that "it is indeed a uniform distribution" so I wanted to point it out.

teddyh|5 years ago

To make a dice roll uniform, wouldn’t it be easier to do randomNumber % 8 and only use it if it is from 0 to 5, and discard and re-roll otherwise?

(Since we can reasonably assume that randomNumber is a binary number, and thus would be balanced over 8 values instead of 6.)

atg_abhishek|5 years ago

Ah very true, an often ignored thing, thank you for sharing!