Random Number Generation

Random number generation in modern NumPy is handled by a Generator object, created with np.random.default_rng(), that produces random floats, integers, and samples from statistical distributions on demand.

Learn Random Number Generation in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll create a generator, draw random floats and integers, sample from a normal distribution, pick and shuffle elements, and lock in reproducible results with a fixed seed.

Modern NumPy creates randomness through a Generator object. You build one with np.random.default_rng(seed) and then call methods on it. Passing a fixed integer seed makes the sequence reproducible — the same seed always yields the same numbers.

Expected output: with seed 42, both calls print the identical array [0.77395605 0.43887844 0.85859792] . Change the seed and the numbers change.

For whole numbers use rng.integers(low, high, size) . Like Python's range , the high value is excluded by default. The size argument controls how many you get and can even be a tuple for a 2D grid.

Expected output: five values in the range 1–6 and a 2×3 grid of values 0–9. With seed 0 these are reproducible; the exact numbers stay fixed as long as the seed does.

Real-world data often follows a bell curve. rng.normal(mean, std, size) draws samples from a normal (Gaussian) distribution with the mean and standard deviation you choose. Drawing many samples, the average will sit close to the requested mean.

Expected output: shape (1000,) , a mean near 100.0 and a standard deviation near 15.0 . The exact decimals depend on the seed, but they cluster around those targets.

rng.choice picks random elements from an array, with or without replacement. rng.shuffle reorders an array in place . You may still see the older np.random.seed and np.random.rand in tutorials — they work, but the Generator API is preferred for new code.

Expected output: two distinct colors, a shuffled 1–5 deck, and from the legacy seed-42 call the fixed pair [0.37454012 0.95071431] .

Replace each ___ so the program creates a seeded generator and rolls four dice.

Expected output: four numbers from 1 to 6 and True . (Answers: default_rng , 7 .)

❌ Results are different every run when I wanted them fixed

✅ Fix: pass a fixed integer, e.g. np.random.default_rng(42) , for reproducible output.

It never should — high is excluded by default, so this usually means an off-by-one expectation.

✅ Fix: to include the top value, pass endpoint=True , or set high one above your target.

Flip a fair coin 1000 times and count the heads. With enough flips the proportion should sit near 0.5.

Lesson 14 complete — randomness, under control!

You can build a seeded generator with default_rng , draw floats, integers, and normal samples, pick and shuffle elements, and guarantee reproducible results with a fixed seed.

🚀 Up next: Linear Algebra — multiply matrices and solve systems with numpy.linalg .

Practice quiz

How do you create a generator in modern NumPy?

  • np.random.default_rng(seed)
  • np.random.make()
  • np.Generator()
  • np.rng()

Answer: np.random.default_rng(seed). np.random.default_rng(seed) is the recommended way to create a Generator.

How do you make random results reproducible?

  • Call shuffle twice
  • Pass a fixed integer seed to default_rng
  • Use a larger array
  • Never call random()

Answer: Pass a fixed integer seed to default_rng. A fixed seed always produces the same sequence, essential for reproducibility.

What does rng.random() return?

  • Integers
  • Booleans
  • Floats in the half-open interval [0, 1)
  • Strings

Answer: Floats in the half-open interval [0, 1). rng.random returns floats uniformly distributed in [0, 1).

By default, is the high value excluded in rng.integers(low, high)?

  • No, it is always included
  • Only for size>1
  • Only for negative numbers
  • Yes, high is excluded by default

Answer: Yes, high is excluded by default. Like Python's range, rng.integers excludes high by default unless endpoint=True.

Which method draws samples from a normal distribution?

  • rng.normal(mean, std, size)
  • rng.integers
  • rng.random
  • rng.choice

Answer: rng.normal(mean, std, size). rng.normal(mean, std, size) draws Gaussian samples with the given mean and spread.

What does rng.shuffle(arr) do?

  • Returns a sorted copy
  • Reorders the array in place and returns None
  • Returns a new shuffled array
  • Picks one element

Answer: Reorders the array in place and returns None. rng.shuffle reorders the array in place and returns None.

Which method picks random elements from an array?

  • rng.normal
  • rng.integers
  • rng.choice
  • rng.shuffle

Answer: rng.choice. rng.choice selects random elements, with or without replacement.

Which is the legacy global API you may still see?

  • rng.integers
  • default_rng
  • rng.normal
  • np.random.seed and np.random.rand

Answer: np.random.seed and np.random.rand. np.random.seed/np.random.rand are the older global functions; default_rng is preferred.

Two generators created with the same seed will...

  • Produce the same sequence of numbers
  • Produce opposite numbers
  • Always differ
  • Raise an error

Answer: Produce the same sequence of numbers. The same seed yields the identical sequence, which is what makes results reproducible.

How do you include the top value in rng.integers?

  • Set size larger
  • Pass endpoint=True
  • Use rng.random instead
  • Subtract one from low

Answer: Pass endpoint=True. Pass endpoint=True (or set high one above your target) to include the top value.