Simulation sampling
Create an explicit random-number stream for simulations or statistical modeling.
NumPy · Function
Creates a NumPy Generator using the default PCG64 BitGenerator. An optional seed initializes its state for reproducible sequences; without one, operating-system entropy is used.
Calling default_rng constructs a Generator backed by NumPy’s default BitGenerator, currently PCG64. Pass a seed or another supported seed source to initialize its state; with no seed, the operating system supplies nondeterministic entropy. Use the returned Generator’s methods for distributions and random values.
numpy.random.default_rng(seed=None)| Name | Type | Description |
|---|---|---|
seed | None, int, array_like[ints], SeedSequence, BitGenerator, Generator | Seed used to initialize the BitGenerator. Accepted forms include None, int, array_like of ints, SeedSequence, BitGenerator, or Generator; it is optional. |
Generator — the initialized generator object.
import numpy as np
rng = np.random.default_rng(12345)
print(rng.integers(0, 10, size=3))
print(rng.random())Creates a reproducible generator and uses it for integer and floating-point samples.
Create an explicit random-number stream for simulations or statistical modeling.
Repeat experiments with identical random values by supplying a fixed integer seed.
Keep random state localized by passing a Generator to functions that need sampling.
It constructs and returns a Generator using NumPy’s default BitGenerator, currently PCG64. The returned object supplies methods for drawing samples from distributions.
Follow-up: How do you make a run reproducible? Pass the same seed when constructing the generator, then consume values in the same order.
With no seed, initialization obtains nondeterministic entropy from the operating system. Consequently, separate executions generally produce different sequences.
Follow-up: What happens when seed is an integer? It initializes the generator state, allowing the resulting sequence to be reproduced when the same seed and draw order are used.
The seed may be None, an integer, an integer array-like, SeedSequence, BitGenerator, or Generator. These inputs initialize or provide the generator state.
Follow-up: Why use a Generator instance rather than a global random-state interface? It gives code an explicit random stream that can be passed and controlled independently.
numpy.random.Generatornumpy.random.PCG64numpy.random.SeedSequencenumpy.random.BitGenerator