0
0
NumPydata~20 mins

Setting random seed for reproducibility in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Seed Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of numpy random with fixed seed
What is the output of the following code snippet?
NumPy
import numpy as np
np.random.seed(42)
result = np.random.randint(0, 10, size=5)
print(result.tolist())
A]6 ,4 ,7 ,3 ,6[
B[6, 3, 7, 4, 6]
C6, 3, 7, 4, 6]
D[6, 3, 7, 4, 6
Attempts:
2 left
💡 Hint
Setting the seed fixes the random numbers generated by numpy.
data_output
intermediate
2:00remaining
Number of unique values with and without seed
Consider the following code. How many unique values are in the array 'a' after running it?
NumPy
import numpy as np
np.random.seed(0)
a = np.random.choice([1, 2, 3, 4, 5], size=10, replace=True)
unique_count = len(np.unique(a))
print(unique_count)
A2
B4
C5
D3
Attempts:
2 left
💡 Hint
The seed fixes the random choices, so the unique count is consistent.
🔧 Debug
advanced
2:00remaining
Identify the error in reproducibility code
What error will this code raise when run?
NumPy
import numpy as np
np.random.seed(123)
result = np.random.randint(0, 5, size='10')
print(result)
ANo error, prints an array of 10 integers
BValueError: size must be positive
CSyntaxError: invalid syntax
DTypeError: 'str' object cannot be interpreted as an integer
Attempts:
2 left
💡 Hint
Check the type of the 'size' argument in randint.
🚀 Application
advanced
2:00remaining
Reproducible random sampling from a DataFrame
You have a DataFrame with 100 rows. You want to randomly select 10 rows reproducibly. Which code snippet achieves this?
Adf.sample(n=10, random_state=42)
Bdf.sample(n=10).set_seed(42)
Cdf.sample(n=10, seed=42)
Ddf.sample(n=10, random_seed=42)
Attempts:
2 left
💡 Hint
Check the correct parameter name for reproducible sampling in pandas.
🧠 Conceptual
expert
2:00remaining
Effect of setting seed on multiple random generators
If you set np.random.seed(0) and then use both numpy and Python's built-in random module to generate random numbers, which statement is true?
ASetting np.random.seed(0) only affects numpy's random functions, not Python's random module.
BSetting np.random.seed(0) also fixes the output of Python's random module.
CPython's random module uses numpy's seed internally, so both are fixed.
DNeither numpy nor Python's random module are affected by np.random.seed(0).
Attempts:
2 left
💡 Hint
Consider if numpy and Python's random share the same seed state.