Is Pythons random.randint statistically random?

前端 未结 6 2123
悲哀的现实
悲哀的现实 2020-12-16 06:00

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,

6条回答
  •  感情败类
    2020-12-16 06:27

    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.zip from 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/random uses 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:

    import random
    ba = bytearray([random.randint(0,255) for n in xrange(1000000)])
    with open('randint.dat', 'w+') as f:
        f.write(ba)
    

    Then I ran the ent program from Fourmilab on it:

    Entropy = 7.999840 bits per byte.
    
    Optimum compression would reduce the size
    of this 1000000 byte file by 0 percent.
    
    Chi square distribution for 1000000 samples is 221.87, and randomly
    would exceed this value 93.40 percent of the times.
    
    Arithmetic mean value of data bytes is 127.5136 (127.5 = random).
    Monte Carlo value for Pi is 3.139644559 (error 0.06 percent).
    Serial correlation coefficient is -0.000931 (totally uncorrelated = 0.0).
    

    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 ent calls this value "almost suspect".

    As a contrast, here is the same analysis of 10 MiB from FreeBSD's Yarrow prng that I ran earlier:

    Entropy = 7.999982 bits per byte.
    
    Optimum compression would reduce the size
    of this 10485760 byte file by 0 percent.
    
    Chi square distribution for 10485760 samples is 259.03, and randomly
    would exceed this value 41.80 percent of the times.
    
    Arithmetic mean value of data bytes is 127.5116 (127.5 = random).
    Monte Carlo value for Pi is 3.139877754 (error 0.05 percent).
    Serial correlation coefficient is -0.000296 (totally uncorrelated = 0.0).
    

    While there seems not much difference in the other data, the χ² precentage is much closer to 50%.

提交回复
热议问题