我试图在random.random()安装了不同python3版本的不同系统上重现python的随机序列.
这应该很容易,因为文档说:
大多数随机模块的算法和种子函数都会在Python版本中发生变化,但保证两个方面不会改变:
- 如果添加了新的播种方法,则将提供向后兼容的播种机.
- 当兼容的播种机被赋予相同的种子时,生成器的random()方法将继续产生相同的序列.
所以我希望下面的代码总是打印相同的10个数字,无论具体的python3版本如何:
import sys
print(sys.version)
from random import seed, random
seed(str(1))
for i in range(10):
print(random())
Run Code Online (Sandbox Code Playgroud)
但是,在两台不同的机器上进行测试:
3.2.3 (default, May 3 2012, 15:51:42)
[GCC 4.6.3]
0.4782479962566343
0.044242767098090496
0.11703586901195051
0.8566892547933538
0.2926790185279551
0.0067328440779825804
0.0013279506360178717
0.22167546902173108
0.9864945747444945
0.5157002525757287
Run Code Online (Sandbox Code Playgroud)
和
3.1.2 (release31-maint, Dec 9 2011, 20:59:40)
[GCC 4.4.5]
0.0698436845523
0.27772471476
0.833036057868
0.35569897036
0.36366158783
0.722487971761
0.963133581734
0.263723867191
0.451002768569
0.0998765577881
Run Code Online (Sandbox Code Playgroud)
给出不同的结果.
为什么是这样?有没有办法让它工作(即两次获得相同的随机序列?)