Deterministic legacy tests
Reproduce results in older code that uses NumPy’s module-level random functions.
NumPy · Function
Reseeds NumPy’s shared legacy RandomState used by module-level random functions. Prefer a dedicated Generator for new code.
Calling this function reseeds NumPy’s shared, legacy RandomState used by random-module generation functions. With the same seed, subsequent draws from that global state are reproducible; when no seed is supplied, the state is initialized from operating-system randomness or the clock. For new code, prefer an independent Generator.
numpy.random.seed(seed=None)| Name | Type | Description |
|---|---|---|
seed | int or 1-D array_like, optional | Seed for the singleton RandomState. If omitted or None, initialize from nondeterministic system entropy or the clock. |
None
import numpy as np
np.random.seed(123)
first = np.random.random(3)
np.random.seed(123)
second = np.random.random(3)
assert np.array_equal(first, second)
print(first)Resetting the legacy global random state to the same seed reproduces the same sequence.
Reproduce results in older code that uses NumPy’s module-level random functions.
Reset a legacy random sequence before a repeatable simulation or example.
Initialize the shared RandomState when maintaining code written against NumPy’s older random API.
numpy.random.Generatornumpy.random.RandomStatenumpy.random.default_rng