我注意到,如果我用一个元组来种子Python的PRNG,每次我都会得到不同的结果。也就是说,Python 3.4程序:
import random
seed1 = ('Foo', 'Bar')
random.seed(seed1)
print(random.random())
每次运行时打印一个不同的数字。这是因为所获取的种子是元组seed1
的id
,每次都不同吗?在
使用元组作为PRNG种子的最佳方法是什么,这样我就能得到可重复的结果?只是random.seed(str(seed1))
?在
Tags:
有趣。在
如果您这样做:
每次运行随机序列时,会得到不同的值。在
如果您这样做:
^{pr2}$在Python解释器的不同运行中,它是相同的series 1=>;n。在
从上一个question:
所以使用Python2.x时,运行
hash('Foo', 'Bar')
通常每次在同一台计算机上都会返回相同的结果,这会给您提供相同的初始种子。在python3.3+上,对元组运行hash
,每次都会给您一个唯一的值。在如果您想得到与python3.3+一致的结果,请查看
hashlib
。例如:也就是说,你会有一个一致的种子,但是由于随机模块在它们的实现上有所不同,你仍然会得到一个不同的随机数。在
相关问题 更多 >
编程相关推荐