[Tutor] printing the random seed?

Kent Johnson kent37 at tds.net
Thu Feb 2 12:03:33 CET 2006


Danny Yoo wrote:
> 
> On Thu, 2 Feb 2006, kevin parks wrote:
> 
> 
>>Danny (hope you are good!) & co,
>>
>>I see that biz about random.seed()... but in the absence of setting that
>>... does it just grab a value from the system clock?
> 
> 
> Yes.  Here's what the documentation says officially:
> 
> """current system time is also used to initialize the generator when the
> module is first imported"""

As of Python 2.4, random.seed() will attempt to use os.urandom() to 
initialize the seed; if that is not available it uses the system time.

>>Is there a way to just let it generate it's usual, known seed... but
>>then observe what that is in case you get an especially good run of
>>data?
> 
> 
> We can call seed() explicitely using system time then when we start using
> the random module, and if the results are interesting, we report that
> initial seed value too.  That way, by knowing the initial conditions, we
> can reproduce the results.

Here is the code from random.py that initializes the seed (a):
             try:
                 a = long(_hexlify(_urandom(16)), 16)
             except NotImplementedError:
                 import time
                 a = long(time.time() * 256) # use fractional seconds

Kent



More information about the Tutor mailing list