Random and fork

Julien Le Goff julien.legoff at gmail.com
Wed Feb 6 17:28:24 CET 2013


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()



More information about the Python-list mailing list