Course map
Stage 1: Foundations
Stage 2: Machine Learning
Stage 3: Deep Learning
Stage 4: LLMs & Generative AI
Stage 5: Production & Specialization

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 — a Generator object you pass around. The older global np.random.seed(...) still works but mixes state across your whole program; prefer the generator.

🧪 Quick quiz

  1. 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.

Search the guide