Python的random:如果我不使用seed(someValue),会发生什么?

2024-04-19 20:17:56 发布

您现在位置:Python中文网/ 问答频道 /正文

a)在这种情况下,随机数生成器是否在每次运行时使用系统时钟(使种子发生变化)?

b)种子是否用于生成指数变量(lambda)的伪随机值?


Tags: lambda系统情况指数时钟种子
3条回答

current system time is used; current system time is also used to initialize the generator when the module is first imported. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).

Random Docs

“使用来源,卢克!”。。。;-). 学习https://svn.python.org/projects/python/trunk/Lib/random.py会很快让你安心;-)。

当种子没有设定时会发生什么(这是“我没有”的情况):

if a is None:
    try:
        a = long(_hexlify(_urandom(16)), 16)
    except NotImplementedError:
        import time
        a = long(time.time() * 256) # use fractional seconds

指数变量:

random = self.random
u = random()
while u <= 1e-7:
    u = random()
return -_log(u)/lambd

很明显,使用的是与其他方法相同的底层随机生成器,因此也同样受到播种或缺乏播种的影响(真的,否则怎么做呢?-)

a)它通常使用系统时钟,某些系统上的时钟可能只有ms精度,因此种子两次非常快可能导致相同的值。

seed(self, a=None) Initialize internal state from hashable object.

None or no argument seeds from current time or from an operating
system specific randomness source if available.

http://pydoc.org/2.5.1/random.html#Random-seed

b)我想expovariate可以,但我找不到任何证据。如果没有,那就太傻了

相关问题 更多 >