Random Seed (NumPy)

A seed is the starting value that initializes NumPy's random number generator. Setting the same seed makes the 'random' sequence reproducible, so your results can be repeated exactly on every run.

NumPy

Pseudo-random numbers are produced by a deterministic algorithm from an internal state; the seed sets that initial state. With the same seed you get the same stream of numbers, which is what lets you reproduce experiments, debug, and write tests against random code.

In the modern API you pass the seed when you create a Generator: np.random.default_rng(42). The legacy global API uses np.random.seed(0), which configures the shared global generator before you call np.random.rand and friends. The modern per-Generator seeding is preferred because it avoids the hidden global state.

Seeding does not make numbers less random for any practical purpose; it only makes the sequence repeatable. Omit the seed (or use a fresh entropy source) when you genuinely want different numbers each run.

Syntax

np.random.default_rng(42)  |  np.random.seed(0)  # legacy

Examples

Python

import numpy as np
np.random.seed(0)
print(np.random.rand(3))

Output:

[0.5488135  0.71518937 0.60276338]

Learn it in a lesson

See also

Back to the Dictionary