np.random.seed()
Seeding fixes the starting point of the random generator so you get the same "random" numbers every time.
In this page:
Syntax
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()
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/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Seeding after generating numbers
- Assuming the seed value itself matters
- 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: