Random and fork

J

Julien Le Goff

Hi everyone,

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.

See below a small program that illustrates this. It is easily fixed, but I'm interested in knowing why this happens. Can anybody give me a hint? Thanks!

import random
import os

for i in xrange(10):
pid = os.fork()
if pid == 0:
# uncommenting this fixes the problem
# random.seed(os.getpid())
print random.random()
os._exit(0)

os.wait()
 
P

Peter Otten

Julien said:
Hi everyone,

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.

See below a small program that illustrates this. It is easily fixed, but
I'm interested in knowing why this happens. Can anybody give me a hint?
Thanks!

Those numbers are "pseudo"-random numbers -- the sequence of numbers
generated is fully determined by the state of the random number generator.
That state like anything else is copied by fork(). I you need "better"
randomness you can use random.SystemRandom:
import random
import os

random = random.SystemRandom()
 
A

Alain Ketterlin

Julien Le Goff said:
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.
 
S

Stephane Wirtel

* Julien Le Goff said:
Hi everyone,

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.

See below a small program that illustrates this. It is easily fixed, but I'm interested in knowing why this happens. Can anybody give me a hint? Thanks!

import random
import os

for i in xrange(10):
pid = os.fork()
if pid == 0:
# uncommenting this fixes the problem
# random.seed(os.getpid())
print random.random()
os._exit(0)

os.wait()

If you look at the code of gunicorn, you can see than there is a
random.seed() just after the fork syscall.

Try with that.

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top