Challenge - 5 Problems
Monte Carlo Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple Monte Carlo estimation of π
What is the output of this code that estimates π using Monte Carlo simulation with 1000 points?
NumPy
import numpy as np np.random.seed(0) points = 1000 x = np.random.uniform(-1, 1, points) y = np.random.uniform(-1, 1, points) inside_circle = (x**2 + y**2) <= 1 pi_estimate = 4 * np.sum(inside_circle) / points print(round(pi_estimate, 2))
Attempts:
2 left
💡 Hint
Think about how many points fall inside the unit circle compared to total points.
✗ Incorrect
The code generates 1000 random points in a square from -1 to 1 on both axes. It counts how many fall inside the unit circle. The ratio multiplied by 4 estimates π. With seed 0, the estimate rounds to 3.14.
❓ data_output
intermediate2:00remaining
Number of points inside circle in Monte Carlo simulation
After running this Monte Carlo simulation code with 5000 points, what is the number of points inside the unit circle?
NumPy
import numpy as np np.random.seed(1) points = 5000 x = np.random.uniform(-1, 1, points) y = np.random.uniform(-1, 1, points) inside_circle = (x**2 + y**2) <= 1 count_inside = np.sum(inside_circle) print(count_inside)
Attempts:
2 left
💡 Hint
Count how many points satisfy the circle condition.
✗ Incorrect
With seed 1 and 5000 points, the sum of points inside the circle is 3932.
❓ visualization
advanced3:00remaining
Visualizing Monte Carlo points inside and outside the circle
Which option shows the correct scatter plot of 1000 Monte Carlo points with points inside the unit circle in blue and outside in red?
NumPy
import numpy as np import matplotlib.pyplot as plt np.random.seed(2) points = 1000 x = np.random.uniform(-1, 1, points) y = np.random.uniform(-1, 1, points) inside_circle = (x**2 + y**2) <= 1 plt.figure(figsize=(5,5)) plt.scatter(x[inside_circle], y[inside_circle], color='blue', s=5, label='Inside Circle') plt.scatter(x[~inside_circle], y[~inside_circle], color='red', s=5, label='Outside Circle') plt.legend() plt.title('Monte Carlo Points Inside and Outside Unit Circle') plt.show()
Attempts:
2 left
💡 Hint
Check colors and labels for inside vs outside points.
✗ Incorrect
The code colors points inside the circle blue and outside red, with a legend and title. Option D matches this exactly.
🧠 Conceptual
advanced1:30remaining
Understanding Monte Carlo simulation error behavior
Which statement correctly describes how the error in Monte Carlo estimation changes as the number of random points increases?
Attempts:
2 left
💡 Hint
Think about how randomness averages out with more samples.
✗ Incorrect
Monte Carlo error typically decreases with the square root of the number of samples, meaning more points reduce error but with diminishing returns.
🔧 Debug
expert2:00remaining
Identify the error in Monte Carlo simulation code
What error will this code raise when run, and why?
NumPy
import numpy as np np.random.seed(3) points = 1000 x = np.random.uniform(-1, 1, points) y = np.random.uniform(-1, 1, points) inside_circle = (x**2 + y**2) <= 1 count_inside = np.sum(inside_circle) print(count_inside)
Attempts:
2 left
💡 Hint
Check the shape and type of y compared to x.
✗ Incorrect
y is generated as an array like x. There is no error, and the code prints the count of points inside the circle.