random numbers, threads and reproducibility 
Author Message
 random numbers, threads and reproducibility

Greetings!

        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), so the sequence of calls to the
whrandom... functions would not be the same.

        A possible solution that came to me is to pass a random seed
(based on a random number) to each thread as it is created, and
instantiate a different whrandom object in each one.

        The question is: is there a better way to do this in Python?

        Thanks for your time.

                                        -- Eloy R. Sanz

_____________________________________________________________________

------------------ http://www.*-*-*.com/ ~ma1satae ----------------------

Crdoba _ Espa?a _______________ Debian 2.2 GNU/Linux 2.2.19 rabinf50



Wed, 05 Nov 2003 16:44:08 GMT  
 random numbers, threads and reproducibility
[eloy]

Quote:
>    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.

Quote:
> 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.

Quote:
>    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.

Quote:
> 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



Wed, 05 Nov 2003 17:54:59 GMT  
 random numbers, threads and reproducibility

Quote:
>> 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.

[snip astute observation regarding threads and randomness].

One way around this particular problem is to use cooperative
multithreading with your own scheduler. Without any preemptive
multitasking involved, the unfolding of your simulation will be
completely deterministic.

C//



Wed, 05 Nov 2003 23:46:18 GMT  
 random numbers, threads and reproducibility
Requiring a threaded program to be 100% reproducable is a tall order.
There are a lot of factors involved.  If your program simply requires that
each thread gets the same 'random' number sequence, then I would
implement this by giving each thread its own whrandom instance, which
 is what you sugest below :-)

Neil


Quote:

> Greetings!

>    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), so the sequence of calls to the
> whrandom... functions would not be the same.

>    A possible solution that came to me is to pass a random seed
> (based on a random number) to each thread as it is created, and
> instantiate a different whrandom object in each one.

>    The question is: is there a better way to do this in Python?

>    Thanks for your time.

>                                         -- Eloy R. Sanz

> _____________________________________________________________________

> ------------------ http://www.uco.es/~ma1satae ----------------------

> Crdoba _ Espa?a _______________ Debian 2.2 GNU/Linux 2.2.19 rabinf50



Wed, 05 Nov 2003 15:41:54 GMT  
 random numbers, threads and reproducibility

Quote:

>Requiring a threaded program to be 100% reproducable is a tall order.
>There are a lot of factors involved.  If your program simply requires that
>each thread gets the same 'random' number sequence, then I would
>implement this by giving each thread its own whrandom instance, which
> is what you sugest below :-)

This won't help. Interactions between preemptively-multitasking threads
cannot be predicted because each thread can be subject to influences
outside of the scope of the entire program _asymmetrically_, thus making
the overall outcome non-deterministic. The play between the threads and
other programs on the machine in essence becomes a sort of random
number source in the program; the different results you see in situations
like this can be subtle: changes in ordering, differing outcomes in
resource-contention, thread syncronization unpredictability, and so forth.

On multiple cpu machines, the typical solution that modelers use to take
advantage of many cpus is "batch-run monte carlo simulation," where
multiple instances of the simulation are run on each cpu. While indeed
no one program runs any faster due to the multiple cpus, the aggregate
completes more quickly, allowing a higher rate of data collection.

C//



Thu, 06 Nov 2003 06:37:09 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Random Number Generator to produce SAME random number from 12:00am-11:59pm

2. Random number not random?

3. Pentium III processor number and random number generator

4. Finding out number of threads or number of open windows in the app

5. Thread Problems - Random Segfaults

6. Binary Reproducibility (same src => diff executable)

7. Binary Reproducibility (same src => dif

8. Tk crashes with PNG images (request for reproducibility)

9. J random-number generator: what is used?

10. Quasi Random numbers

11. How do I get random numbers with ALC?

12. [VA] Random Numbers!

 

 
Powered by phpBB® Forum Software