基于 randint returns 的抛硬币大多是错误的?

Coin flip based on randint returns false mostly?

我正在 python 使用 Pygame 编写游戏。在游戏中,我使用以下语句将变量设置为 True 或 False 的随机值:

(False, True)[random.randint(0, 1)]

运行这样2到3次后,一直返回False。起初,我以为这可能是巧合,但在 运行 这样的 10 次之后,它只返回了一次 True。

这只是一个巧合,还是存在这种差异背后的实际原因?

以防万一你需要知道这一点,我正在使用内置的随机库

纯属巧合。随机机会的本质是样本量越小,您就越有可能获得看起来“非随机”的结果。幸运的是,使用计算机程序可以很容易地对非常大的样本量进行测试。

>>> import random
>>> import collections
>>> collections.Counter((False, True)[random.randint(0, 1)] for _ in range(10))
Counter({False: 6, True: 4})
>>> collections.Counter((False, True)[random.randint(0, 1)] for _ in range(100))
Counter({False: 52, True: 48})
>>> collections.Counter((False, True)[random.randint(0, 1)] for _ in range(1000))
Counter({True: 528, False: 472})
>>> collections.Counter((False, True)[random.randint(0, 1)] for _ in range(10000))
Counter({False: 5047, True: 4953})
>>> collections.Counter((False, True)[random.randint(0, 1)] for _ in range(100000))
Counter({False: 50288, True: 49712})
>>> collections.Counter((False, True)[random.randint(0, 1)] for _ in range(1000000))
Counter({True: 500047, False: 499953})

抛硬币的次数越多,分布越趋于均匀。