[Numpy-discussion] How do I seed the radom number generator?
robert.kern at gmail.com
Thu Jun 22 15:33:43 EDT 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 : import numpy as np
In : rs = np.random.RandomState([12345678, 90123456, 78901234])
In : rs.rand(5)
Out: array([ 0.40355172, 0.27449337, 0.56989746, 0.34767024, 0.47185004])
In : np.random.RandomState.seed?
Base Class: <type 'method_descriptor'>
String Form: <method 'seed' of 'mtrand.RandomState' objects>
Seed the generator.
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
"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