So I’m testing an calculating the probabilities of certain dice rolls, for a game.
The base case if that rolling one 10sided die.
I did a million samples of this, and ended up with the following proportions:
Result
0 0.000000000000000%
1 10.038789961210000%
2 10.043589956410000%
3 9.994890005110000%
4 10.025289974710000%
5 9.948090051909950%
6 9.965590034409970%
7 9.990190009809990%
8 9.985490014509990%
9 9.980390019609980%
10 10.027589972410000%
These should of course all be 10%.
There is a standard deviation of 0.0323207% in these results.
that, to me, seems rather high.
Is it just coincidence?
As I understand it the random module accesses proper pseudo-random numbers.
Ie ones from a method that pass the statistical tests to be random.
Or are these pseudo-pseudo-random number generators
Should I be using cryptographic pseudo-random number generators?
I’m fairly sure I don’t need a true random number generator (see http://www.random.org/, http://en.wikipedia.org/wiki/Hardware_random_number_generator).
I am currently regenerating all my results with 1 billion samples,
(cos why not, I have a crunchy server at my disposal, and some sleep to do)
Martijn’s answer is a pretty succinct review of the random number generators that Python has access to.
If you want to check out the properties of the generated pseudo-random data, download
random.zipfrom http://www.fourmilab.ch/random/, and run it on a big sample of random data. Especially the χ² (chi squared) test is very sensitive to randomness. For a sequence to be really random, the percentage from the χ² test should be between 10% and 90%.For a game I’d guess that the Mersenne Twister that Python uses internally should be sufficiently random (unless you’re building an online casino :-).
If you want pure randomness, and if you are using Linux, you can read from
/dev/random. This only produces random data from the kernel’s entropy pool (which is gathered from the unpredictable times that interrupts arrive), so it will block if you exhaust it. This entropy is used to initialize (seed) the PRNG used by/dev/urandom. On FreeBSD, the PRNG that supplies data for/dev/randomuses the Yarrow algorithm, which is generally regarded as being cryptographically secure.Edit: I ran some tests on bytes from
random.randint. First creating a million random bytes:Then I ran the
entprogram from Fourmilab on it:Now for the χ² test, the further you get from 50%, the more suspect the data is. If one is very fussy, values <10% or >90% are deemed unacceptable. John Walker, author of
entcalls this value “almost suspect”.As a contrast, here is the same analysis of 10 MiB from FreeBSD’s Yarrow prng that I ran earlier:
While there seems not much difference in the other data, the χ² precentage is much closer to 50%.