Generate Random Numbers with NumPy
Use NumPy's Generator to draw random integers and floats, and a seed to get the same numbers every run.
- np.random.default_rng(seed)
- integers(), random() and normal()
- Why seeds make results repeatable
- 1
seed = 42Fix the starting point - 2
integers(1, 11, size=5)Draw five integers - 3
[1 8 7 5 5]Same result every run
A seeded generator
The problem
Simulations, games, sampling and tests all need random numbers. For testing and teaching you also want the same "random" numbers on every run, so results can be checked and repeated.
Think of it like this
A seeded random generator is like a machine that rolls a 10-sided die, starting from a number you set. Set the same start number (the seed) and it replays exactly the same series of rolls every time. Change the seed and the series changes. Like any die, it can roll the same number twice.
default_rng(seed=42)🎛️ Set the machine's start number to 42integers(low=1, high=11, size=5)🎲 Roll five numbers from 1 to 10high=11🚫 The high value itself is never rolledvalues📋 [1 8 7 5 5], the same on every runA random number generator produces values that look random; giving it a seed makes it produce the same sequence every time, which makes results repeatable.
Key points
np.random.default_rng(seed=42)creates a generator with a fixed seed.integers(low=1, high=11, size=5)draws five whole numbers from 1 to 10.highis excluded, so 11 is never drawn.- Because of the seed,
valuesis[1 8 7 5 5]on every run; without a seed it would change.
When should you use it?
- Simulations such as dice games or random walks.
- Random samples and train/test splits in data science.
- Repeatable tests and examples, by fixing the seed.
When not to use it
- Passwords, tokens or anything security related: use the
secretsmodule. - A single random choice in plain Python: the
randommodule is enough.
Example code
import numpy as np
generator = np.random.default_rng(seed=42)
values = generator.integers(low=1, high=11, size=5)
print(values)
Result of running the code
[1 8 7 5 5]
Code explanation
Default_rng creates the recommended Generator object. The fixed seed makes this example deterministic across repeated runs.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input shape and dtype.
- Try negative, floating-point, and missing values.
- Apply the operation to larger multidimensional arrays and measure performance.
Common mistakes
Thinking high is included
integers(1, 10) never returns 10.
generator.integers(low=1, high=10, size=5) # 1 to 9
generator.integers(low=1, high=11, size=5) # 1 to 10
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Epsilon-Greedy Multi-Armed Bandit Balance exploration and exploitation on a multi-armed bandit with epsilon-greedy action selection. Easy
- Temperature and Top-K Sampling Choose the next token like a language model does: keep the top k logits, apply temperature, and sample. Medium