Random and fork

Discussion in 'Python' started by Julien Le Goff, Feb 6, 2013.

  1. 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()
     
    Julien Le Goff, Feb 6, 2013
    #1
    1. Advertising

  2. Julien Le Goff

    Peter Otten Guest

    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()
     
    Peter Otten, Feb 6, 2013
    #2
    1. Advertising

  3. 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.
     
    Alain Ketterlin, Feb 6, 2013
    #3
  4. 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.
     
    Julien Le Goff, Feb 7, 2013
    #4
  5. * 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
     
    Stephane Wirtel, Feb 7, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lars-Erik Aabech
    Replies:
    8
    Views:
    845
    Lars-Erik Aabech
    Apr 28, 2005
  2. Joona I Palaste

    random() and fork()

    Joona I Palaste, Dec 7, 2003, in forum: C Programming
    Replies:
    2
    Views:
    383
    Joona I Palaste
    Dec 7, 2003
  3. globalrev
    Replies:
    4
    Views:
    771
    Gabriel Genellina
    Apr 20, 2008
  4. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    574
    Eric Snow
    Jan 8, 2009
  5. VK
    Replies:
    15
    Views:
    1,174
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page