随机:默认种子是什么? [英] random: what is the default seed?
问题描述
对于 Python 3,我可以在互联网上找到许多不同的地方,说明 random
模块的默认种子是基于系统时间的.
For Python 3, I can find many different places on the internet stating that the default seed for the random
module is based on system time.
Python 2.7 也是这种情况吗?我想是的,因为如果我启动两个不同的 Python 进程,并且在这两个进程中我都执行 import random;random.random()
那么两个不同的进程返回不同的结果.
Is this also the case for Python 2.7? I imagine it is, because if I start two different Python processes, and in both I do import random; random.random()
then the two different processes return different results.
如果确实使用系统时间,那么实际使用的种子是多少?(例如自午夜以来的秒数"或自 UNIX 纪元以来的微秒数",或...)如果不是,那么用什么来播种 PRNG?
If it does use system time, what is the actual seed used? (E.g. "number of seconds since midnight" or "number of microseconds since UNIX epoch", or ...) If not, what is used to seed the PRNG?
推荐答案
这是关于如何为 Random
对象生成默认种子的源代码.
This is the source code about how to generate default seed for a Random
object.
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
的更多信息,请查看此 页.
urandom
equals to os.urandom
. And for more information about urandom
, please check this page.
这篇关于随机:默认种子是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!