Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'fixes': 0.05; 'seemed': 0.07; 'python': 0.09; 'fixed,': 0.09; 'happens.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'fork().': 0.16; 'generator.': 0.16; 'hint?': 0.16; 'illustrates': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.17; 'copied': 0.17; 'everyone,': 0.17; 'import': 0.21; 'random': 0.24; 'header:User-Agent:1': 0.26; 'thanks!': 0.26; 'header:X-Complaints-To:1': 0.28; 'behaviour': 0.29; 'this.': 0.29; "i'm": 0.29; 'related': 0.30; 'expect': 0.31; 'anybody': 0.32; 'print': 0.32; 'problem': 0.33; 'to:addr:python-list': 0.33; 'program,': 0.34; 'sequence': 0.35; 'received:org': 0.36; 'but': 0.36; 'anything': 0.36; 'why': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'to:addr:python.org': 0.39; 'easily': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'today': 0.67 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Random and fork Date: Wed, 06 Feb 2013 17:47:38 +0100 Organization: None References: <9efb072f-b671-45da-bc4e-ef224b2ffe76@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084936f.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360169270 news.xs4all.nl 6853 [2001:888:2000:d::a6]:52184 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!feed.ac-versailles.fr!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:38294 Julien Le Goff wrote: > 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() > 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()