Random Generator#
The Generator provides access to
a wide range of distributions, and served as a replacement for
RandomState. The main difference between
the two is that Generator relies on an additional BitGenerator to
manage state and generate the random bits, which are then transformed into
random values from useful distributions. The default BitGenerator used by
Generator is PCG64. The BitGenerator
can be changed by passing an instantized BitGenerator to Generator.
- numpy.random.default_rng(seed=None)#
Construct a new Generator with the default BitGenerator (PCG64).
- Parameters:
- seed{None, int, array_like[ints], SeedSequence, BitGenerator, Generator, RandomState}, optional
A seed to initialize the
BitGenerator. If None, then fresh, unpredictable entropy will be pulled from the OS. If anintorarray_like[ints]is passed, then all values must be non-negative and will be passed toSeedSequenceto derive the initialBitGeneratorstate. One may also pass in aSeedSequenceinstance. Additionally, when passed aBitGenerator, it will be wrapped byGenerator. If passed aGenerator, it will be returned unaltered. When passed a legacyRandomStateinstance it will be coerced to aGenerator.
- Returns:
- Generator
The initialized generator object.
Notes
If
seedis not aBitGeneratoror aGenerator, a newBitGeneratoris instantiated. This function does not manage a default global instance.See Seeding and entropy for more information about seeding.
Examples
default_rngis the recommended constructor for the random number classGenerator. Here are several ways we can construct a random number generator usingdefault_rngand theGeneratorclass.Here we use
default_rngto generate a random float:>>> import numpy as np >>> rng = np.random.default_rng(12345) >>> print(rng) Generator(PCG64) >>> rfloat = rng.random() >>> rfloat 0.22733602246716966 >>> type(rfloat) <class 'float'>
Here we use
default_rngto generate 3 random integers between 0 (inclusive) and 10 (exclusive):>>> import numpy as np >>> rng = np.random