← Back to NumPy Course | Chapter 9: Random Module | Lesson 1 of 6

np.random.seed()

Seeding fixes the starting point of the random generator so you get the same "random" numbers every time.

In this page:

  1. np.random.seed()
Syntax
python
np.random.seed(seed)

np.random.seed()

np.random.seed(n) sets the state of NumPy's legacy global generator. Calling it before generating numbers makes results repeatable, which is vital for tests and tutorials. Newer code often prefers default_rng(seed) for isolated generators.

Note: Reseeding with the same number restarts the same sequence.

Example: np.random.seed()

python
import numpy as np

np.random.seed(0)
print(np.random.rand(3).round(3))
np.random.seed(0)
print(np.random.rand(3).round(3))

# Output:
# [0.549 0.715 0.603]
# [0.549 0.715 0.603]
Related Topics
Common Mistakes
  1. Seeding after generating numbers
  2. Assuming the seed value itself matters
  3. Relying on global state in large programs
Chapter Summary
  • seed makes results repeatable
  • Same seed gives same sequence
  • Call it before generating
  • default_rng avoids global state
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.