Run NumPy Online

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.

  • Intermediate
  • 2 min read
  • 1 runnable example
What you'll learn
  • np.random.default_rng(seed)
  • integers(), random() and normal()
  • Why seeds make results repeatable
default_rng(seed=42)
  1. 1seed = 42Fix the starting point
  2. 2integers(1, 11, size=5)Draw five integers
  3. 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.

From a dice machine to code
default_rng(seed=42)🎛️ Set the machine's start number to 42
integers(low=1, high=11, size=5)🎲 Roll five numbers from 1 to 10
high=11🚫 The high value itself is never rolled
values📋 [1 8 7 5 5], the same on every run
Same seed, same sequence.
In one sentence

A 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

  1. np.random.default_rng(seed=42) creates a generator with a fixed seed.
  2. integers(low=1, high=11, size=5) draws five whole numbers from 1 to 10.
  3. high is excluded, so 11 is never drawn.
  4. Because of the seed, values is [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 secrets module.
  • A single random choice in plain Python: the random module is enough.

Example code

Run code →
main.py
import numpy as np

generator = np.random.default_rng(seed=42)
values = generator.integers(low=1, high=11, size=5)

print(values)
Get a Run button for this example

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.

  1. Change the input shape and dtype.
  2. Try negative, floating-point, and missing values.
  3. Apply the operation to larger multidimensional arrays and measure performance.
Run in the Python compiler →

Common mistakes

Thinking high is included

integers(1, 10) never returns 10.

Problem
generator.integers(low=1, high=10, size=5)  # 1 to 9
Fix
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.

All challenges