Part 01 / Topic 5 of 7
Randomness and seeds
ML is full of randomness: shuffled data, random splits, random initial weights. A seed makes it reproducible — the same seed always gives the same numbers.
import numpy as np
r1 = np.random.default_rng(42).integers(0, 10, 5)
r2 = np.random.default_rng(42).integers(0, 10, 5)
print(r1) # [0 7 6 4 4]
print((r1 == r2).all()) # True — same seed, same numbers
Every example in this course seeds its generator. That is why the outputs printed in these lessons will match yours exactly.
🔍 Going deeper:
np.random.default_rng(seed)is the modern API — aGeneratorobject you pass around. The older globalnp.random.seed(...)still works but mixes state across your whole program; prefer the generator.
🧪 Quick quiz
- What does
rng = np.random.default_rng(42)give you?
Show the answer
A reproducible random number generator: the same seed gives the same numbers. That makes experiments repeatable.
Done here? The practice set for Part 1 — 4 exercises with solutions — is on the part page.