Bird
Raised Fist0
NumPydata~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of setting a random seed in numpy?
easy
A. To increase the range of random numbers
B. To speed up random number generation
C. To generate only positive random numbers
D. To make random number results repeatable

Solution

  1. Step 1: Understand what a random seed does

    Setting a random seed fixes the starting point for random number generation.
  2. Step 2: Effect of fixed seed on results

    This means the same sequence of random numbers is produced every time the code runs.
  3. Final Answer:

    To make random number results repeatable -> Option D
  4. Quick Check:

    Random seed = repeatable results [OK]
Hint: Seed fixes randomness to get same results every run [OK]
Common Mistakes:
  • Thinking seed speeds up random generation
  • Believing seed changes number range
  • Assuming seed makes numbers positive only
2. Which of the following is the correct syntax to set a random seed to 42 in numpy?
easy
A. np.random.seed(42)
B. np.seed.random(42)
C. np.random.set_seed(42)
D. np.random.seed = 42

Solution

  1. Step 1: Recall the correct function name and usage

    The correct function to set seed in numpy is np.random.seed().
  2. Step 2: Check the argument and syntax

    The seed value is passed as an integer inside the parentheses, like np.random.seed(42).
  3. Final Answer:

    np.random.seed(42) -> Option A
  4. Quick Check:

    Correct function call = np.random.seed(42) [OK]
Hint: Use np.random.seed(number) exactly [OK]
Common Mistakes:
  • Swapping function and module names
  • Using incorrect function like set_seed
  • Assigning seed instead of calling function
3. What will be the output of the following code?
import numpy as np
np.random.seed(0)
print(np.random.randint(1, 10))
np.random.seed(0)
print(np.random.randint(1, 10))
medium
A. Different numbers both times
B. 5 followed by different number
C. 5 followed by 5
D. Error due to repeated seed setting

Solution

  1. Step 1: Understand effect of setting seed before each random call

    Setting the seed to 0 resets the random number generator to the same start point.
  2. Step 2: Predict output of np.random.randint(1, 10) after resetting seed

    Both calls produce the same random integer because the seed is reset before each call.
  3. Final Answer:

    5 followed by 5 -> Option C
  4. Quick Check:

    Same seed = same random number [OK]
Hint: Resetting seed repeats same random number [OK]
Common Mistakes:
  • Assuming different numbers after resetting seed
  • Thinking repeated seed causes error
  • Ignoring seed reset effect
4. Identify the error in this code snippet that tries to set a random seed:
import numpy as np
np.random.seed = 123
print(np.random.rand())
medium
A. Missing import statement
B. Seed is assigned instead of called as a function
C. Seed value must be a float, not integer
D. np.random.rand() is incorrect function

Solution

  1. Step 1: Check how seed is set

    The code assigns 123 to np.random.seed instead of calling it as a function.
  2. Step 2: Understand consequence of assignment

    This overwrites the seed function with an integer, causing errors on later random calls.
  3. Final Answer:

    Seed is assigned instead of called as a function -> Option B
  4. Quick Check:

    Use np.random.seed(123), not assignment [OK]
Hint: Call seed() as function, don't assign it [OK]
Common Mistakes:
  • Assigning seed instead of calling
  • Using wrong random function
  • Ignoring import statement
5. You want to generate a reproducible array of 3 random floats between 0 and 1 using numpy. Which code snippet achieves this correctly?
hard
A. np.random.seed(7) arr = np.random.rand(3) print(arr)
B. np.random.seed = 7 arr = np.random.rand(3) print(arr)
C. np.random.set_seed(7) arr = np.random.rand(3) print(arr)
D. np.random.seed(7) arr = np.random.randint(3) print(arr)

Solution

  1. Step 1: Set the random seed correctly

    Use np.random.seed(7) to fix the random sequence.
  2. Step 2: Generate 3 random floats between 0 and 1

    np.random.rand(3) creates an array of 3 floats in [0,1).
  3. Final Answer:

    np.random.seed(7) arr = np.random.rand(3) print(arr) -> Option A
  4. Quick Check:

    Correct seed + rand(3) = reproducible floats [OK]
Hint: Seed with np.random.seed(), use np.random.rand(3) [OK]
Common Mistakes:
  • Assigning seed instead of calling
  • Using non-existent set_seed function
  • Using randint instead of rand for floats