Setting a random seed makes sure you get the same random numbers every time you run your code. This helps when you want to share your work or check your results later.
Setting random seed for reproducibility in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NumPy
import numpy as np np.random.seed(your_seed_number)
Replace your_seed_number with any integer you choose.
Calling np.random.seed() sets the starting point for random number generation.
Examples
NumPy
import numpy as np np.random.seed(42) print(np.random.rand(3))
NumPy
import numpy as np np.random.seed(0) print(np.random.randint(1, 10, size=5))
Sample Program
This program sets the seed to 123, then generates and prints 5 random numbers. Running it multiple times will print the same numbers.
NumPy
import numpy as np # Set seed for reproducibility np.random.seed(123) # Generate 5 random numbers between 0 and 1 random_numbers = np.random.rand(5) print("Random numbers:", random_numbers)
Important Notes
Setting the seed affects all random functions in numpy until you change it again.
Use the same seed number to get the same random results every time.
Summary
Setting a random seed makes your random results repeatable.
Use np.random.seed(number) before generating random numbers.
This helps when sharing or testing your code.
Practice
1. What is the main purpose of setting a random seed in
numpy?easy
Solution
Step 1: Understand what a random seed does
Setting a random seed fixes the starting point for random number generation.Step 2: Effect of fixed seed on results
This means the same sequence of random numbers is produced every time the code runs.Final Answer:
To make random number results repeatable -> Option DQuick 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
Solution
Step 1: Recall the correct function name and usage
The correct function to set seed in numpy isnp.random.seed().Step 2: Check the argument and syntax
The seed value is passed as an integer inside the parentheses, likenp.random.seed(42).Final Answer:
np.random.seed(42) -> Option AQuick 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
Solution
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.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.Final Answer:
5 followed by 5 -> Option CQuick 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
Solution
Step 1: Check how seed is set
The code assigns 123 tonp.random.seedinstead of calling it as a function.Step 2: Understand consequence of assignment
This overwrites the seed function with an integer, causing errors on later random calls.Final Answer:
Seed is assigned instead of called as a function -> Option BQuick 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
Solution
Step 1: Set the random seed correctly
Usenp.random.seed(7)to fix the random sequence.Step 2: Generate 3 random floats between 0 and 1
np.random.rand(3)creates an array of 3 floats in [0,1).Final Answer:
np.random.seed(7) arr = np.random.rand(3) print(arr) -> Option AQuick 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
