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


Groups > comp.lang.python > #38292

Random and fork

X-Received by 10.224.110.68 with SMTP id m4mr15449193qap.2.1360168104376; Wed, 06 Feb 2013 08:28:24 -0800 (PST)
X-Received by 10.49.62.2 with SMTP id u2mr2489256qer.38.1360168104310; Wed, 06 Feb 2013 08:28:24 -0800 (PST)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!p13no17408785qai.0!news-out.google.com!k2ni8440qap.0!nntp.google.com!p13no15737784qai.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Wed, 6 Feb 2013 08:28:24 -0800 (PST)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=80.28.250.164; posting-account=gpBI-woAAAC8p_P9iCoAQLtMsfa1MHAb
NNTP-Posting-Host 80.28.250.164
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <9efb072f-b671-45da-bc4e-ef224b2ffe76@googlegroups.com> (permalink)
Subject Random and fork
From Julien Le Goff <julien.legoff@gmail.com>
Injection-Date Wed, 06 Feb 2013 16:28:24 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.python:38292

Show key headers only | 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