栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Pythons random.randint统计上是随机的吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pythons random.randint统计上是随机的吗?

Martijn的答案是对Python可以访问的随机数生成器的简洁总结。

如果要检查生成的伪随机数据的属性,请

random.zip
从http://www.fourmilab.ch/random/下载,然后对大量随机数据进行运行。特别是χ²(卡方)检验对随机性非常敏感。为了使序列真正随机,来自χ²检验的百分比应在10%到90%之间。

对于游戏,我猜想Python内部使用的Mersenne Twister应该足够随机(除非您要建立在线赌场:-)。

如果您想要
随机性,并且您正在使用Linux,则可以从中阅读

/dev/random
。这只会从内核的熵池中生成随机数据(该数据是从中断到达的不可预测的时间收集的),因此,如果用尽了它将阻塞。该熵用于初始化(种子)所使用的PRNG
/dev/urandom
。在FreeBSD上,提供数据供
/dev/random
使用的PRNG使用Yarrow算法,该算法通常被认为是加密安全的。

编辑: 我对中的字节进行了一些测试

random.randint
。首先创建一百万个随机字节:

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

然后,我

ent
从Fourmilab上运行了该程序:

Entropy = 7.999840 bits per byte.Optimum compression would reduce the sizeof this 1000000 byte file by 0 percent.Chi square distribution for 1000000 samples is 221.87, and randomlywould 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).

现在,对于χ²检验,您从50%得到的数据越多,对数据的怀疑就越大。如果非常挑剔,则将<10%或> 90%的值视为不可接受。作者约翰·沃克(John
Walker)

ent
将此值称为“几乎可疑”。

相比之下,这与我之前运行的FreeBSD的Yarrow prng对10 MiB的分析相同:

Entropy = 7.999982 bits per byte.Optimum compression would reduce the sizeof this 10485760 byte file by 0 percent.Chi square distribution for 10485760 samples is 259.03, and randomlywould 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).

尽管其他数据似乎没有太大差异,但χ²百分比 接近50%。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/660973.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号