Challenge - 5 Problems
Random Sampling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of numpy random choice with replace=False
What is the output of this code snippet?
import numpy as np np.random.seed(0) sample = np.random.choice([10, 20, 30, 40, 50], size=3, replace=False) print(sample)
NumPy
import numpy as np np.random.seed(0) sample = np.random.choice([10, 20, 30, 40, 50], size=3, replace=False) print(sample)
Attempts:
2 left
💡 Hint
Remember that setting the seed fixes the random output.
✗ Incorrect
With seed 0 and replace=False, numpy picks 3 unique elements in a fixed order. The output is [20 40 50].
❓ data_output
intermediate1:30remaining
Shape of random normal samples array
What is the shape of the array produced by this code?
import numpy as np samples = np.random.normal(loc=5, scale=2, size=(4,3)) print(samples.shape)
NumPy
import numpy as np samples = np.random.normal(loc=5, scale=2, size=(4,3)) print(samples.shape)
Attempts:
2 left
💡 Hint
The size parameter defines the shape of the output array.
✗ Incorrect
The size=(4,3) means 4 rows and 3 columns, so shape is (4, 3).
❓ visualization
advanced2:30remaining
Histogram of uniform random samples
Which option shows the correct histogram plot for 1000 samples from a uniform distribution between 0 and 1?
NumPy
import numpy as np import matplotlib.pyplot as plt np.random.seed(1) samples = np.random.uniform(0, 1, 1000) plt.hist(samples, bins=10, edgecolor='black') plt.show()
Attempts:
2 left
💡 Hint
Uniform distribution means equal chance for all values in the range.
✗ Incorrect
Uniform samples spread evenly, so histogram bars are roughly equal height.
🧠 Conceptual
advanced1:30remaining
Effect of random seed on reproducibility
Why do we set a random seed before generating random samples in numpy?
Attempts:
2 left
💡 Hint
Think about reproducibility in experiments.
✗ Incorrect
Setting a seed fixes the random number generator state, so results repeat exactly.
🔧 Debug
expert2:00remaining
Identify the error in random sample generation
What error does this code raise?
import numpy as np np.random.seed(2) sample = np.random.choice([1, 2, 3], size=5, replace=False) print(sample)
NumPy
import numpy as np np.random.seed(2) sample = np.random.choice([1, 2, 3], size=5, replace=False) print(sample)
Attempts:
2 left
💡 Hint
Check if sample size is larger than population when replace=False.
✗ Incorrect
Sampling 5 unique items from a list of 3 without replacement is impossible, causing ValueError.