Puzzled about random initialisation

Robin Becker robin at reportlab.com
Thu Jul 8 09:36:00 EDT 2004


We've been queried about the randomness of some filenames we're producing.
I worked through and seemed to satisfy myself that random is being initialised 
from the system time in C
	time(&now)
	init_genrand(self, (unsigned long)now);

where now is a time expressed in integral seconds since the eopoch.

This would mean that our filenames are not particularly random as that would 
make the sequences common if two cgi scripts started within a second of each other.

However, when I use these trivial scripts

rem doit.bat
python bimbo.py
python bimbo.py
python bimbo.py
python bimbo.py
python bimbo.py
python bimbo.py
python bimbo.py
python bimbo.py

#bimbo.py
import time, random
print time.time(), hex(random.randint(0,0xffffffffl))


I get dirrent draws for each run even when the system time doessn't differ bu 
much. I'm puzzled exactly how the random generator is being initialised.

C:\TMP>timethis doit.bat

TimeThis :  Command Line :  doit.bat
TimeThis :    Start Time :  Thu Jul 08 14:25:20 2004


C:\TMP>python bimbo.py
1089293120.59 0x74e18bc1

C:\TMP>python bimbo.py
1089293120.67 0x9EECC786L

C:\TMP>python bimbo.py
1089293120.77 0x75d734d6

C:\TMP>python bimbo.py
1089293120.83 0x509afece

C:\TMP>python bimbo.py
1089293120.91 0x1edde1aa

C:\TMP>python bimbo.py
1089293120.98 0xE552E511L

C:\TMP>python bimbo.py
1089293121.06 0xC2C91369L

C:\TMP>python bimbo.py
1089293121.14 0xF739FCFEL

TimeThis :  Command Line :  doit.bat
TimeThis :    Start Time :  Thu Jul 08 14:25:20 2004
TimeThis :      End Time :  Thu Jul 08 14:25:21 2004
TimeThis :  Elapsed Time :  00:00:00.656

-- 
Robin Becker



More information about the Python-list mailing list