random:什么是默认种子?

时间:2017-09-27 08:46:27

标签: python python-2.7

对于Python 3,我可以在互联网上找到许多不同的地方,说明random模块的默认种子是基于系统时间。

Python 2.7也是如此吗?我想是这样,因为如果我启动两个不同的Python进程,并且在{I} import random; random.random(),那么两个不同的进程会返回不同的结果。

如果它确实使用了系统时间,那么实际使用的种子是什么? (例如“自午夜以来的秒数”或“自UNIX时代以来的微秒数”,或......) 如果没有,用于播种PRNG的是什么?

1 个答案:

答案 0 :(得分:2)

这是关于如何为Random对象生成默认种子的源代码。

try:
    # Seed with enough bytes to span the 19937 bit
    # state space for the Mersenne Twister
    a = long(_hexlify(_urandom(2500)), 16)
except NotImplementedError:
    import time
    a = long(time.time() * 256) # use fractional seconds

urandom等于os.urandom。有关urandom的详细信息,请查看page

相关问题