🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.random.seed()

AI & DATA SCIENCE // np-random-seed

np.random.seed() sets the starting state of NumPy's legacy global random number generator, making subsequent 'random' calls fully reproducible.

Syntax

np.random.seed(seed_value)

Deep Dive Course

Computers can't generate truly random numbers algorithmically — np.random's functions are pseudo-random, deterministically computed from an internal state that evolves with each call. Calling np.random.seed(n) resets that internal state to a fixed, known starting point derived from n, so every subsequent random call produces the exact same sequence of values every time the program runs with that same seed — invaluable for writing reproducible tests, tutorials, and debugging, though obviously not appropriate for anything requiring genuine unpredictability, like security tokens.

1Understanding np.random.seed()

Computers can't generate truly random numbers algorithmically — np.random's functions are pseudo-random, deterministically computed from an internal state that evolves with each call. Calling np.random.seed(n) resets that internal state to a fixed, known starting point derived from n, so every subsequent random call produces the exact same sequence of values every time the program runs with that same seed — invaluable for writing reproducible tests, tutorials, and debugging, though obviously not appropriate for anything requiring genuine unpredictability, like security tokens.

💡

Call np.random.seed() once, near the start of a script or test, rather than repeatedly before every individual random call — reseeding partway through a sequence of calls can accidentally make some of them correlated or repeat values you didn't intend.

editor.html
import numpy as np

np.random.seed(42)
print(np.random.rand(3))
np.random.seed(42)
print(np.random.rand(3))
localhost:3000

2Practical Example

Here is a real-world application of np.random.seed() showing how it is used in production NumPy code.

editor.html
import numpy as np

np.random.seed(1)
first_run = np.random.randint(0, 100, 3)
np.random.seed(1)
second_run = np.random.randint(0, 100, 3)
print(np.array_equal(first_run, second_run))
localhost:3000

3Best Practices

Follow these guidelines when working with np.random.seed():

1. Set a seed once at the start of a script/test for full reproducibility, rather than seeding before every individual call

2. Never rely on np.random (seeded or not) for anything security-sensitive, like tokens or passwords — use the secrets module instead

3. Prefer the newer np.random.default_rng(seed) Generator API over the legacy global np.random.seed() in new code, since it avoids relying on shared global state

⚠️

Tip: Call np.random.seed() once, near the start of a script or test, rather than repeatedly before every individual random call — reseeding partway through a sequence of calls can accidentally make some of them correlated or repeat values you didn't intend.

editor.html
import numpy as np

np.random.seed(42)
print(np.random.rand(3))
np.random.seed(42)
print(np.random.rand(3))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

np.random.seed(42)
print(np.random.rand(3))
np.random.seed(42)
print(np.random.rand(3))
Example 02Advanced Example
import numpy as np

np.random.seed(1)
first_run = np.random.randint(0, 100, 3)
np.random.seed(1)
second_run = np.random.randint(0, 100, 3)
print(np.array_equal(first_run, second_run))

Best Practices

  • Set a seed once at the start of a script/test for full reproducibility, rather than seeding before every individual call
  • Never rely on np.random (seeded or not) for anything security-sensitive, like tokens or passwords — use the secrets module instead
  • Prefer the newer np.random.default_rng(seed) Generator API over the legacy global np.random.seed() in new code, since it avoids relying on shared global state

Interview Question

Why does calling np.random.seed(42) twice, with a random call in between each, produce the exact same sequence of 'random' values both times?

Hint: Think about what 'pseudo-random' actually means for a computer.

np.random's number generator isn't truly random — it's a deterministic algorithm that transforms an internal numeric state into the next value on every call, then updates that state for the next call. Calling np.random.seed(42) resets that internal state to the exact same fixed starting point every time it's called with that value, so every subsequent call, being a deterministic function of that same starting state, is guaranteed to produce the identical sequence of outputs, no matter how many times the seed is reset back to 42.

Exercises

MediumPractice using np.random.seed() in a real scenario.
View Solution
import numpy as np

np.random.seed(42)
print(np.random.rand(3))
np.random.seed(42)
print(np.random.rand(3))

Frequently Asked Questions

Why does calling np.random.seed(42) twice, with a random call in between each, produce the exact same sequence of 'random' values both times?

np.random's number generator isn't truly random — it's a deterministic algorithm that transforms an internal numeric state into the next value on every call, then updates that state for the next call. Calling np.random.seed(42) resets that internal state to the exact same fixed starting point every time it's called with that value, so every subsequent call, being a deterministic function of that same starting state, is guaranteed to produce the identical sequence of outputs, no matter how many times the seed is reset back to 42.

Related Functions

np-random-randnp-random-randnnp-random-normal