python 版本之间的种子随机数是否不一致?
Are seeded random numbers not consistent between python versions?
问题:
为什么 python 版本之间的种子随机输出看似不同?我是否漏掉了一些明显的东西,它是否是记录在案的行为(我找不到)
观测结果:
尝试在 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
输出:
([8, 22, 27, 35, 45, 47],
[10, 18, 20, 29, 45, 47],
[4, 7, 15, 22, 47, 52],
[5, 8, 37, 40, 50, 55])
比较点:
而来自 Raymond Hettinger 在 Europycon 2011 上的高级 python 的屏幕截图显示了不同的输出 - 可能是 python 2.6 或 2.7:(图像质量很差,但结果明显不同)
查看 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.
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.
这似乎足以回答屏幕截图和您的案例之间不同顺序的事实。
问题:
为什么 python 版本之间的种子随机输出看似不同?我是否漏掉了一些明显的东西,它是否是记录在案的行为(我找不到)
观测结果:
尝试在 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
输出:
([8, 22, 27, 35, 45, 47],
[10, 18, 20, 29, 45, 47],
[4, 7, 15, 22, 47, 52],
[5, 8, 37, 40, 50, 55])
比较点:
而来自 Raymond Hettinger 在 Europycon 2011 上的高级 python 的屏幕截图显示了不同的输出 - 可能是 python 2.6 或 2.7:(图像质量很差,但结果明显不同)
查看 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.
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.
这似乎足以回答屏幕截图和您的案例之间不同顺序的事实。