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
Recall & Review
beginner
What does setting a random seed in numpy do?
Setting a random seed makes sure that the random numbers generated are the same every time you run the code. This helps in getting reproducible results.
Click to reveal answer
beginner
How do you set a random seed in numpy?
You use np.random.seed(your_number). Replace your_number with any integer to fix the sequence of random numbers.
Click to reveal answer
beginner
Why is reproducibility important in data science?
Reproducibility means you or others can run the same code and get the same results. This is important for checking work, sharing findings, and debugging.
Click to reveal answer
beginner
What happens if you don't set a random seed before generating random numbers?
If you don't set a seed, numpy will generate different random numbers each time you run the code, which can make results vary and be hard to reproduce.
Click to reveal answer
beginner
Example: How to generate 5 random numbers between 0 and 1 with a fixed seed?
```python
import numpy as np
np.random.seed(42)
rands = np.random.rand(5)
print(rands)
```
This will always print the same 5 random numbers.
Click to reveal answer
What function sets the random seed in numpy?
Anumpy.set_seed()
Bnumpy.random.set()
Cnumpy.random.seed()
Dnumpy.seed.random()
✗ Incorrect
The correct function to set the seed is numpy.random.seed().
Why should you set a random seed in your code?
ATo avoid using random numbers
BTo make random numbers different every time
CTo speed up random number generation
DTo make random numbers reproducible
✗ Incorrect
Setting a seed makes random numbers reproducible, so results stay the same each run.
What type of value do you pass to numpy.random.seed()?
AAn integer
BA float
CA string
DA boolean
✗ Incorrect
You pass an integer to numpy.random.seed() to fix the random sequence.
If you run numpy.random.rand(3) twice without setting a seed, what happens?
AYou get different 3 numbers each time
BYou get zeros
CYou get an error
DYou get the same 3 numbers both times
✗ Incorrect
Without a seed, random numbers differ each run.
Which of these is a benefit of reproducibility in data science?
ACode runs faster
BResults can be trusted and checked
CRandom numbers are more random
DYou don't need to write comments
✗ Incorrect
Reproducibility helps others trust and verify your results.
Explain how to set a random seed in numpy and why it is useful.
Think about how to get the same random numbers every time.
You got /4 concepts.
Describe what happens if you do not set a random seed before generating random numbers in numpy.
Consider the effect on results when running code multiple times.
You got /4 concepts.
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
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 D
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
Step 1: Recall the correct function name and usage
The correct function to set seed in numpy is np.random.seed().
Step 2: Check the argument and syntax
The seed value is passed as an integer inside the parentheses, like np.random.seed(42).
Final Answer:
np.random.seed(42) -> Option A
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
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 C
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
Step 1: Check how seed is set
The code assigns 123 to np.random.seed instead 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 B
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
Step 1: Set the random seed correctly
Use np.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 A
Quick Check:
Correct seed + rand(3) = reproducible floats [OK]
Hint: Seed with np.random.seed(), use np.random.rand(3) [OK]