Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

random: what is the default seed?

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.

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.

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?

like image 723
acdr Avatar asked Sep 27 '17 08:09

acdr


1 Answers

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 equals to os.urandom. And for more information about urandom, please check this page.

like image 56
Sraw Avatar answered Nov 12 '22 06:11

Sraw