Challenge - 5 Problems
Seed Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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())
Attempts:
2 left
💡 Hint
Setting the seed fixes the random numbers generated by numpy.
✗ Incorrect
Using np.random.seed(42) ensures the random numbers generated are always the same. The randint function with these parameters produces [6, 3, 7, 4, 6].
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
The seed fixes the random choices, so the unique count is consistent.
✗ Incorrect
With seed 0, the random choice picks values so that all 5 unique numbers appear at least once in the array.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check the type of the 'size' argument in randint.
✗ Incorrect
The size parameter must be an integer or tuple of integers, not a string. Passing '10' causes a TypeError.
🚀 Application
advanced2: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?
Attempts:
2 left
💡 Hint
Check the correct parameter name for reproducible sampling in pandas.
✗ Incorrect
The pandas sample method uses 'random_state' to set the seed for reproducibility.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Consider if numpy and Python's random share the same seed state.
✗ Incorrect
Numpy and Python's random module have separate random number generators and seeds. Setting numpy's seed does not affect Python's random module.