[Numpy-discussion] Is there anyway to seed random numbers without global state?

Robert Kern robert.kern@gmail....
Mon Jan 12 13:50:20 CST 2009

On Mon, Jan 12, 2009 at 13:41, Tom Denniston
<tom.denniston@alum.dartmouth.org> wrote:
> 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.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

More information about the Numpy-discussion mailing list