Python的随机性:如果我不使用seed(someValue)会发生什么? [英] Python's random: What happens if I don't use seed(someValue)?

查看:86
本文介绍了Python的随机性:如果我不使用seed(someValue)会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a)在这种情况下,随机数生成器是否在每次运行时都使用系统的时钟(进行种子更改)?

b)种子是否用于生成expovariate(λ)的伪随机值?

解决方案

使用源代码,卢克!" ... ;-).研究 https://svn.python.org/projects/python/trunk/Lib/random.py 将迅速使您放心;-).

未设置种子时会发生什么情况(即"i is None"的情况):

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)In this case does the random number generator uses the system's clock (making the seed change) on each run?

b)Is the seed used to generate the pseudo-random values of expovariate(lambda)?

解决方案

"Use the Source, Luke!"...;-). Studying https://svn.python.org/projects/python/trunk/Lib/random.py will rapidly reassure you;-).

What happens when seed isn't set (that's the "i is None" case):

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

and the expovariate:

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

obviously uses the same underlying random generator as every other method, and so is identically affected by the seeding or lack thereof (really, how else would it have been done?-)

这篇关于Python的随机性:如果我不使用seed(someValue)会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆