Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.random.default_rng

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.

How it works

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.

Syntax

numpy.random.default_rng(seed=None)

Parameters

NameTypeDescription
seedNone, int, array_like[ints], SeedSequence, BitGenerator, GeneratorSeed used to initialize the BitGenerator. Accepted forms include None, int, array_like of ints, SeedSequence, BitGenerator, or Generator; it is optional.

Returns

Generator — the initialized generator object.

Key points

  • The function creates a new Generator backed by PCG64, NumPy’s default BitGenerator.
  • Its optional seed argument determines the initial state or supplies an existing random-state source.
  • The return value is the initialized Generator used to request random samples.
  • Providing an integer seed enables reproducible deterministic sequences.
  • Positive integers of any size are valid integer seeds.
  • The intended workflow is to create one Generator, then call its sampling methods.

Common mistakes

  • Omitting the seed makes runs produce different sequences because initialization uses operating-system entropy.
  • Do not use this generator for security or cryptographic purposes.

Example: Seed a generator and draw samples

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.

When to use it

Simulation sampling

Create an explicit random-number stream for simulations or statistical modeling.

Reproducible experiments

Repeat experiments with identical random values by supplying a fixed integer seed.

Controlled random streams

Keep random state localized by passing a Generator to functions that need sampling.

Interview questions

QWhat does np.random.default_rng return?

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.

QWhat is the effect of omitting seed?

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.

QWhich seed inputs does default_rng accept?

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.Generator
  • numpy.random.PCG64
  • numpy.random.SeedSequence
  • numpy.random.BitGenerator

Source: Random Generator — NumPy v2.0 Manual. Checked 2026-10-15.