0
0
NumPydata~10 mins

Setting random seed for reproducibility in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Setting random seed for reproducibility
Start
Set random seed
Generate random numbers
Use or save results
Repeat with same seed?
Same random
numbers
Set a fixed seed before generating random numbers to get the same results every time you run the code.
Execution Sample
NumPy
import numpy as np
np.random.seed(42)
r = np.random.rand(3)
print(r)
Set seed 42, generate 3 random numbers, print them. Same output every run.
Execution Table
StepActionSeed StateRandom Numbers GeneratedOutput
1Import numpyNo seed setNoneNo output
2Set seed to 42Seed=42 fixedNoneNo output
3Generate 3 random numbersSeed=42 fixed[0.37454012, 0.95071431, 0.73199394]None
4Print random numbersSeed=42 fixed[0.37454012, 0.95071431, 0.73199394][0.37454012 0.95071431 0.73199394]
5Run again with seed 42Seed=42 fixed[0.37454012, 0.95071431, 0.73199394]Same output as step 4
6Run without setting seedNo seed setRandom different numbersDifferent output
7Run with different seed (e.g. 7)Seed=7 fixedDifferent fixed numbersDifferent output
8EndSeed state unchangedNo new numbersExecution stops
💡 Execution stops after printing random numbers; seed ensures repeatability.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
np.random.seedNot set42424242
r (random numbers)NoneNone[0.37454012, 0.95071431, 0.73199394][0.37454012, 0.95071431, 0.73199394][0.37454012, 0.95071431, 0.73199394]
Key Moments - 3 Insights
Why do we get the same random numbers every time after setting the seed?
Setting the seed fixes the starting point of the random number generator, so it produces the same sequence each run, as shown in execution_table steps 2 to 5.
What happens if we don't set the seed before generating random numbers?
Without setting the seed, the random numbers differ each run because the generator starts from a different state, as seen in execution_table step 6.
Does changing the seed number change the random numbers generated?
Yes, different seeds produce different fixed sequences of random numbers, shown in execution_table step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What random numbers are generated after setting seed 42?
A[0.5, 0.5, 0.5]
B[0.12345678, 0.87654321, 0.56789012]
C[0.37454012, 0.95071431, 0.73199394]
DNo numbers generated
💡 Hint
Check the 'Random Numbers Generated' column at step 3 in execution_table.
At which step does the output become different when the seed is not set?
AStep 5
BStep 6
CStep 4
DStep 7
💡 Hint
Look at the 'Output' column in execution_table for step 6.
If we change the seed from 42 to 7, what happens to the random numbers generated?
AThey become a different fixed sequence
BThey stay exactly the same
CThey become completely random every time
DNo numbers are generated
💡 Hint
Refer to execution_table step 7 'Random Numbers Generated' and 'Output' columns.
Concept Snapshot
Set a random seed with np.random.seed(number) to get repeatable random numbers.
Generating random numbers after setting the seed always produces the same sequence.
Without setting seed, random numbers differ each run.
Changing the seed changes the fixed sequence.
Useful for debugging and sharing reproducible results.
Full Transcript
This visual execution shows how setting a random seed in numpy fixes the starting point of the random number generator. First, numpy is imported. Then, np.random.seed(42) sets the seed to 42. When we generate three random numbers with np.random.rand(3), the output is always the same: [0.37454012, 0.95071431, 0.73199394]. Printing these numbers shows the fixed output. Running the code again with the same seed produces the same numbers. If we do not set the seed, the numbers change each time. Changing the seed to a different number like 7 produces a different fixed sequence. This helps make experiments reproducible and results consistent.