Reproducible experiments
Generate repeatable test fixtures, randomized examples, or simulations.
NumPy · Module
numpy.random provides pseudo-random number generators and distribution sampling for NumPy. The modern workflow is to create a Generator with default_rng, optionally seed it for reproducible results, and call its sampling methods.
The module provides pseudo-random number generators and distribution-sampling utilities. Create a Generator with default_rng, optionally provide a seed for reproducibility, and use its methods to produce scalar or array samples.
numpy.randomimport numpy as np
rng = np.random.default_rng(42)
values = rng.integers(0, 10, size=5)
print(values)Create a reproducible generator with a seed, then draw five uniformly distributed integers from 0 through 9.
Generate repeatable test fixtures, randomized examples, or simulations.
Draw values from uniform and other probability distributions for numerical models.
Create random arrays for algorithms, benchmarks, and exploratory numerical work.
numpy.random supplies pseudo-random number generators and sampling methods for many probability distributions. The recommended pattern is to create a Generator with default_rng and call methods on that object.
Follow-up: Why use a Generator instead of relying on module-level functions? A Generator gives explicit state ownership and supports the modern random-number API.
Pass an integer seed to default_rng, such as default_rng(42), to initialize a reproducible deterministic sequence. Reusing the same seed and generation sequence reproduces the results.
Follow-up: What happens without a seed? The generator is initialized from operating-system data, so repeated runs generally produce different sequences.
The module's generators are intended for statistical modeling and simulation, not cryptographic use. Security-sensitive applications need a security-oriented random source instead.
Follow-up: Would a larger seed make it cryptographically secure? No; changing the seed does not change the generator's suitability for security or cryptography.
numpy.random.default_rngnumpy.random.Generatornumpy.random.integersnumpy.random.random