Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38292 > unrolled thread
| Started by | Julien Le Goff <julien.legoff@gmail.com> |
|---|---|
| First post | 2013-02-06 08:28 -0800 |
| Last post | 2013-02-07 14:29 +0100 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | Julien Le Goff <julien.legoff@gmail.com> |
|---|---|
| Date | 2013-02-06 08:28 -0800 |
| Subject | Random and fork |
| Message-ID | <9efb072f-b671-45da-bc4e-ef224b2ffe76@googlegroups.com> |
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()
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-02-06 17:47 +0100 |
| Message-ID | <mailman.1420.1360169270.2939.python-list@python.org> |
| In reply to | #38292 |
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()
[toc] | [prev] | [next] | [standalone]
| From | Alain Ketterlin <alain@dpt-info.u-strasbg.fr> |
|---|---|
| Date | 2013-02-06 17:49 +0100 |
| Message-ID | <87ip655qwt.fsf@dpt-info.u-strasbg.fr> |
| In reply to | #38292 |
Julien Le Goff <julien.legoff@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Julien Le Goff <julien.legoff@gmail.com> |
|---|---|
| Date | 2013-02-07 01:41 -0800 |
| Message-ID | <7ee26c92-0690-4f16-afe6-ef320c1c8f12@googlegroups.com> |
| In reply to | #38295 |
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 <julien.legoff@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Stephane Wirtel <stephane@wirtel.be> |
|---|---|
| Date | 2013-02-07 14:29 +0100 |
| Message-ID | <mailman.1448.1360243968.2939.python-list@python.org> |
| In reply to | #38292 |
* Julien Le Goff <julien.legoff@gmail.com> [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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web