Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Random and fork

Reply
Thread Tools

Random and fork

 
 
Julien Le Goff
Guest
Posts: n/a
 
      02-06-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()
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      02-06-2013
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()



 
Reply With Quote
 
 
 
 
Alain Ketterlin
Guest
Posts: n/a
 
      02-06-2013
Julien Le Goff <> writes:

> 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.


The random number generator is initialized once, when the module is
first imported. Forking simply duplicates the process in its current
state, so no reinitilization occurs, both (or all) processes' generators
are in the same state, and therefore generate the same sequence.

-- Alain.
 
Reply With Quote
 
Julien Le Goff
Guest
Posts: n/a
 
      02-07-2013
Thank you for the answers! It was much simpler than I thought.

On Wednesday, 6 February 2013 17:49:06 UTC+1, Alain Ketterlin wrote:
> Julien Le Goff <> writes:
>
>
>
> > 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.

>
>
>
> The random number generator is initialized once, when the module is
>
> first imported. Forking simply duplicates the process in its current
>
> state, so no reinitilization occurs, both (or all) processes' generators
>
> are in the same state, and therefore generate the same sequence.
>
>
>
> -- Alain.


 
Reply With Quote
 
Stephane Wirtel
Guest
Posts: n/a
 
      02-07-2013
* Julien Le Goff <> [2013-02-06 08:28:24 -0800]:

> 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()
> --
> http://mail.python.org/mailman/listinfo/python-list


If you look at the code of gunicorn, you can see than there is a
random.seed() just after the fork syscall.

Try with that.

Regards,
--
Stéphane Wirtel - http://wirtel.be - @matrixise
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
os.fork and pty.fork Eric Snow Python 0 01-08-2009 06:32 AM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Random "The IListSource does not contain any datasources" and more (Crashing a live site at random, twice a week or so) Lars-Erik Aabech ASP .Net 8 04-28-2005 07:52 AM
random() and fork() Joona I Palaste C Programming 2 12-07-2003 09:45 PM



Advertisments