random numbers, threads and reproducibility

Tim Peters tim.one at home.com
Sat May 19 05:54:59 EDT 2001


[eloy]
> 	I am developing a small class framework for Genetic Algorithms in
> Python, and wishing to create some threads, I face a problem:
>
> 	One of the requirements I want to keep is reproducibility. A
> genetic algorithm is a stochastic (i.e., random) procedure, and I print
> the random seed at the start of each run of a expreriment, so I can
> reproduce the run later and be sure it will reach the same results. But
> threads are not deterministic (I think),

The full answer to that in Python is very complicated and app-dependent, so
settle for believing they're not deterministic.

> so the sequence of calls to the whrandom... functions would not
> be the same.

Don't use whrandom -- use random instead.  whrandom is going away.

> 	A possible solution that came to me is to pass a random seed
> (based on a random number)

What, you've got your Python hooked up to a Geiger counter <wink>?
Seriously, if you have a source of true random numbers with which to pull off
this trick, use that source directly and forget Python's facilities.  Seeding
an RNG with a number *obtained* from that RNG is illusory progress.

> to each thread as it is created, and instantiate a different
> whrandom object in each one.

How do you know the segments of the RNG's period used by each thread are
pairwise disjoint then?  You don't.  If they aren't disjoint, the chance of
statistical independence is 0, and then your results will most likely be
bogus.  This is difficult stuff.  If you intend to publish based on your
results, you'll have to suck in an expert to help (there are no *adequate*
easy answers here).  Else a better start is to read the docs for Python 2.1's
random module, which has new and better facilities (compared to 2.0 or
earlier):

    http://www.python.org/doc/current/lib/module-random.html





More information about the Python-list mailing list