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

andandandand picture andandandand · May 3, 2009 · Viewed 9.7k times · Source

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

Answer

Alex Martelli picture Alex Martelli · May 3, 2009

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