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> Subject: Random and fork From: Julien Le Goff 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 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()