Random and fork

Alain Ketterlin alain at dpt-info.u-strasbg.fr
Wed Feb 6 17:49:06 CET 2013


Julien Le Goff <julien.legoff at gmail.com> writes:

> Today I came accross a behaviour I did not expect in python (I am
> using 2.7). In my program, random.random() always seemed to return the
> same number; it turned out to be related to the fact that I was using
> os.fork.

The random number generator is initialized once, when the module is
first imported. Forking simply duplicates the process in its current
state, so no reinitilization occurs, both (or all) processes' generators
are in the same state, and therefore generate the same sequence.

-- Alain.



More information about the Python-list mailing list