Numpy’s random number routines produce pseudo random numbers using combinations of a
BitGenerator to create sequences and a
Generator to use those sequences to sample from different statistical distributions:
Since Numpy version 1.17.0 the Generator can be initialized with a number of different BitGenerators. It exposes many different probability distributions. See NEP 19 for context on the updated random Numpy number routines. The legacy
RandomState random number routines are still available, but limited to a single BitGenerator.
# Uses the old numpy.random.RandomState from numpy import random random.standard_normal()
Generator can be used as a replacement for
RandomState. Both class instances now hold a internal
BitGenerator instance to provide the bit stream, it is accessible as
gen.bit_generator. Some long-overdue API cleanup means that legacy and compatibility methods have been removed from
| || ||Notes|
|| ||Compatible with |
|| ||Add an |
| ||removed||Use |
new-or-different for more information
# As replacement for RandomState(); default_rng() instantiates Generator with # the default PCG64 BitGenerator. from numpy.random import default_rng rg = default_rng() rg.standard_normal() rg.bit_generator
Something like the following code can be used to support both
Generator, with the understanding that the interfaces are slightly different
try: rg_integers = rg.integers except AttributeError: rg_integers = rg.randint a = rg_integers(1000)
Seeds can be passed to any of the BitGenerators. The provided value is mixed via
SeedSequence to spread a possible sequence of seeds across a wider range of initialization states for the BitGenerator. Here
PCG64 is used and is wrapped with a
from numpy.random import Generator, PCG64 rg = Generator(PCG64(12345)) rg.standard_normal()
The new infrastructure takes a different approach to producing random numbers from the
RandomState object. Random number generation is separated into two components, a bit generator and a random generator.
BitGenerator has a limited set of responsibilities. It manages state and provides functions to produce random doubles and random unsigned 32- and 64-bit values.
random generator takes the bit generator-provided stream and transforms them into more useful distributions, e.g., simulated normal random values. This structure allows alternative bit generators to be used with little code duplication.
from numpy.random import default_rng rg = default_rng(12345) rg.random()
from numpy.random import Generator, MT19937 rg = Generator(MT19937(12345)) rg.random()
The Box-Muller method used to produce NumPy’s normals is no longer available in
Generator. It is not possible to reproduce the exact random values using Generator for the normal distribution or any other distribution that relies on the normal such as the
RandomState.standard_t. If you require bitwise backward compatible streams, use
dtypeargument that accepts
np.float64to produce either single or double prevision uniform random variables for select distributions
outargument that allows existing arrays to be filled for select distributions
random_entropyprovides access to the system source of randomness that is used in cryptographic applications (e.g.,
ctypes) and CFFI (
cffi). This allows the bit generators to be used in numba.
integersis now the canonical way to generate integer random numbers from a discrete uniform distribution. The
randnmethods are only available through the legacy
endpointkeyword can be used to specify open or closed intervals. This replaces both
randintand the deprecated
randomis now the canonical way to generate floating-point random numbers, which replaces
RandomState.ranf. This is consistent with Python’s
SeedSequenceto convert seeds into initialized states.
See What’s New or Different for a complete list of improvements and differences from the traditional
The included generators can be used in parallel, distributed applications in one of three ways:
This package was developed independently of NumPy and was integrated in version 1.17.0. The original repo is at https://github.com/bashtage/randomgen.
© 2005–2019 NumPy Developers
Licensed under the 3-clause BSD License.