Is there anyway to seed random numbers without global state?
I know how to seed and generate random numbers using: numpy.random.seed and numpy.random.rand The problem is the seeding of the random numbers is global which I would think would make it non-thread safe as well as having all the other annoyances of global state like having so set the seed and set it back when done. I would think the ideal would be to be able to build random number objects where each has a seed so that each object's seed is independent. Does such a thing exist in numpy or scipy. Does this even make sense to people? Is there something about random number generation that makes it inherently a global thing? --Tom
On Mon, Jan 12, 2009 at 13:41, Tom Denniston
I know how to seed and generate random numbers using: numpy.random.seed and numpy.random.rand
The problem is the seeding of the random numbers is global which I would think would make it non-thread safe as well as having all the other annoyances of global state like having so set the seed and set it back when done.
I would think the ideal would be to be able to build random number objects where each has a seed so that each object's seed is independent. Does such a thing exist in numpy or scipy. Does this even make sense to people? Is there something about random number generation that makes it inherently a global thing?
from numpy.random import RandomState prng = RandomState(myseed) prng.standard_normal() prng.uniform(low, high) ... All of the "functions" in numpy.random are just aliases to the methods on a global RandomState object provided for convenience. Whenever you need to control the seed, you should explicitly instantiate a RandomState object and pass it around. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
On Mon, Jan 12, 2009 at 01:41:24PM -0600, Tom Denniston wrote:
I would think the ideal would be to be able to build random number objects where each has a seed so that each object's seed is independent.
You mean something like: In [1]: import numpy as np In [2]: rs = np.random.RandomState(seed=3) In [3]: rs.rand() Out[3]: 0.12671989922202853 :) Gaƫl
participants (3)
-
Gael Varoquaux
-
Robert Kern
-
Tom Denniston