[Numpy-discussion] How do I seed the radom number generator?

Robert Kern robert.kern at gmail.com
Thu Jun 22 14:33:43 CDT 2006


Keith Goodman wrote:
> How do I seed rand and randn?

If you can, please use the .rand() and .randn() methods on a RandomState object 
which you can initialize with whatever seed you like.

In [1]: import numpy as np
rs
In [2]: rs = np.random.RandomState([12345678, 90123456, 78901234])

In [3]: rs.rand(5)
Out[3]: array([ 0.40355172,  0.27449337,  0.56989746,  0.34767024,  0.47185004])

In [5]: np.random.RandomState.seed?
Type:           method_descriptor
Base Class:     <type 'method_descriptor'>
String Form:    <method 'seed' of 'mtrand.RandomState' objects>
Namespace:      Interactive
Docstring:
     Seed the generator.

     seed(seed=None)

     seed can be an integer, an array (or other sequence) of integers of any
     length, or None. If seed is None, then RandomState will try to read data
     from /dev/urandom (or the Windows analogue) if available or seed from
     the clock otherwise.


The rand() and randn() "functions" are actually references to methods on a 
global instance of RandomState. The .seed() method on that object is also 
similarly exposed as numpy.random.seed(). If you are writing new code, please 
explicitly use a RandomState object. Only use numpy.random.seed() if you must 
control code that uses the global rand() and randn() "functions" and you can't 
modify it.

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