Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38292

Random and fork

Newsgroups comp.lang.python
Date 2013-02-06 08:28 -0800
Message-ID <9efb072f-b671-45da-bc4e-ef224b2ffe76@googlegroups.com> (permalink)
Subject Random and fork
From Julien Le Goff <julien.legoff@gmail.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Random and fork Julien Le Goff <julien.legoff@gmail.com> - 2013-02-06 08:28 -0800
  Re: Random and fork Peter Otten <__peter__@web.de> - 2013-02-06 17:47 +0100
  Re: Random and fork Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2013-02-06 17:49 +0100
    Re: Random and fork Julien Le Goff <julien.legoff@gmail.com> - 2013-02-07 01:41 -0800
  Re: Random and fork Stephane Wirtel <stephane@wirtel.be> - 2013-02-07 14:29 +0100

csiph-web