Is Pythons random.randint statistically random?

前端 未结 6 2126
悲哀的现实
悲哀的现实 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:23

    I reran the OP's exercise with one billion iterations:

    from collections import Counter
    import random
    n = 1000000000
    c = Counter(random.randint(1, 10) for _ in xrange(n))
    for i in range(1,11):
        print '%2s  %02.10f%%' % (i, c[i] * 100.0 / n)
    

    Here's the (reformatted) result:

     1     9.9996500000%
     2    10.0011089000%
     3    10.0008568000%
     4    10.0007495000%
     5     9.9999089000%
     6     9.9985344000%
     7     9.9994913000%
     8     9.9997877000%
     9    10.0010818000%
    10     9.9988307000%
    

    See the other answers to this question for their excellent analysis.

提交回复
热议问题