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

NumPy · Function

np.random.seed

Reseeds NumPy’s shared legacy RandomState used by module-level random functions. Prefer a dedicated Generator for new code.

How it works

Calling this function reseeds NumPy’s shared, legacy RandomState used by random-module generation functions. With the same seed, subsequent draws from that global state are reproducible; when no seed is supplied, the state is initialized from operating-system randomness or the clock. For new code, prefer an independent Generator.

Syntax

numpy.random.seed(seed=None)

Parameters

NameTypeDescription
seedint or 1-D array_like, optionalSeed for the singleton RandomState. If omitted or None, initialize from nondeterministic system entropy or the clock.

Returns

None

Key points

  • The function resets NumPy’s shared module-level RandomState.
  • The optional seed argument may be omitted to initialize without an explicitly supplied seed.
  • Generator is the modern related API for independent random-number streams.

Common mistakes

  • It changes the shared legacy random state, so unrelated code using the module-level random functions can be affected.
  • Prefer a Generator for new code instead of reseeding the module-level random state.

Example: Reproduce a legacy random sequence

import numpy as np

np.random.seed(123)
first = np.random.random(3)

np.random.seed(123)
second = np.random.random(3)

assert np.array_equal(first, second)
print(first)

Resetting the legacy global random state to the same seed reproduces the same sequence.

When to use it

Deterministic legacy tests

Reproduce results in older code that uses NumPy’s module-level random functions.

Reproducible demonstrations

Reset a legacy random sequence before a repeatable simulation or example.

Legacy compatibility

Initialize the shared RandomState when maintaining code written against NumPy’s older random API.

  • numpy.random.Generator
  • numpy.random.RandomState
  • numpy.random.default_rng

Source: numpy.random.seed — NumPy v2.5 Manual. Checked 2026-10-15.