简体   繁体   English

python版本之间的种子随机数不一致吗?

[英]Are seeded random numbers not consistent between python versions?

Question: 题:

Why are seeded random outputs seemingly different across python versions? 为什么种子随机输出在python版本之间似乎有所不同? Have I missed something obvious, is it a documented behavior (I could not find it) 我是否错过了明显的事情,是否有记录在案的行为(我找不到)

Observed results: 观察结果:

An attempt to reproduce seeded random numbers across python versions produces different results: 尝试在python版本之间重现种子随机数会产生不同的结果:

# python 3.7
rachel = sorted(Random('rachel').sample(range(57), 6))
larry = sorted(Random('larry').sample(range(57), 6))
armin = sorted(Random('armin').sample(range(57), 6))
laura = sorted(Random('laura').sample(range(57), 6))
rachel, larry, armin, laura

output: 输出:

([8, 22, 27, 35, 45, 47],
 [10, 18, 20, 29, 45, 47],
 [4, 7, 15, 22, 47, 52],
 [5, 8, 37, 40, 50, 55])

Comparison point: 比较点:

Whereas a screenshot from Raymond Hettinger's advanced python at Europycon 2011 is showing a different output - probably python 2.6 or 2.7: (the image quality is poor, but the results are clearly different) 而Raymond Hettinger在Europycon 2011上的高级python的屏幕截图显示了不同的输出-可能是python 2.6或2.7 :(图像质量很差,但是结果明显不同)

在此处输入图片说明

Looking into the documentation for the seed function in the random module you see that there is a note that in Python 3.2 a different version is used by default. 查看random模块中seed函数的文档,您会发现有一条注释,即在Python 3.2中默认使用其他版本。

With version 2 (the default), a str, bytes, or bytearray object gets converted to an int and all of its bits are used. 使用版本2(默认值)时,str,bytes或bytearray对象将转换为int并使用其所有位。

With version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds. 对于版本1(为从旧版本的Python复制随机序列而提供),str和byte的算法生成的种子范围更窄。

This seems enough to answer the fact of different sequences between the screenshot and your case. 这似乎足以回答屏幕截图与您的案例之间顺序不同的事实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM