Re: Puzzled about random initialisation
From: Tim Peters (tim.peters_at_gmail.com)
Date: 07/08/04
- Next message: Peter Hansen: "Thread interrupt revisited (was Re: try: finally: question)"
- Previous message: Rajarshi Guha: "RE: updating a variable / scope problem"
- In reply to: Robin Becker: "Puzzled about random initialisation"
- Next in thread: Robin Becker: "Re: Puzzled about random initialisation"
- Reply: Robin Becker: "Re: Puzzled about random initialisation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 8 Jul 2004 17:00:12 -0400 To: python-list@python.org
[Robin Becker]
> 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.
If you were calling the C code directly, that would be relevant. But
you're not, Instead you're using the default instance of the Random
class Python creates for you, with the initialization *it* uses. That
code is in random.py:
def seed(self, a=None):
...
if a is None:
import time
a = long(time.time() * 256) # use fractional seconds
super(Random, self).seed(a)
time.time() typically changes value only 18.2 times per second on
Windows, so the multiplication by 256 isn't as effective as it looks
on Windows. But you'll still get a different seed on Windows if start
times are more than about 0.055 seconds apart.
All the start times in the example you gave later were at least 0.06
seconds apart, so the different outcomes are expected. Your machine
is fast enough that you might start to see duplicates on Windows if
you ran your Python instances with the -S switch (which skips a bunch
of expensive initialization).
Or, more directly,
import time, random
rd = {}
td = {}
ntries = 0
finish = time.time() + 10
while 1:
t = time.time()
if t > finish:
break
td[t] = 1
rd[random.Random().random()] = 1
ntries += 1
print len(rd), "unique random() values in", ntries, "attempts"
print len(td), "unique time.time() values"
On my WinXP box, that printed
641 unique random() values in 165923 attempts
641 unique time.time() values
Since it ran for 10 seconds, I'm seeing time.time() change about 64
times per second. I believe that's because this is a hyper-threaded
box, Whatever, if I had been able to start ~166000 instance of Python
in that time, I would have seen only ~640 different seedings across
them.
- Next message: Peter Hansen: "Thread interrupt revisited (was Re: try: finally: question)"
- Previous message: Rajarshi Guha: "RE: updating a variable / scope problem"
- In reply to: Robin Becker: "Puzzled about random initialisation"
- Next in thread: Robin Becker: "Re: Puzzled about random initialisation"
- Reply: Robin Becker: "Re: Puzzled about random initialisation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|