Python's random: What happens if I don't use seed(someValue)?

浪尽此生 提交于 2019-11-27 04:45:34

问题


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)?


回答1:


"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?-)




回答2:


a) It typically uses the system clock, the clock on some systems may only have ms precision and so seed twice very quickly may result in the same value.

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) I would imagine expovariate does, but I can't find any proof. It would be silly if it didn't.




回答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://stackoverflow.com/questions/817705/pythons-random-what-happens-if-i-dont-use-seedsomevalue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!