Bird
Raised Fist0
NumPydata~20 mins

Monte Carlo simulation basics in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Monte Carlo Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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))
A3.14
B2.56
C3.00
D4.00
Attempts:
2 left
💡 Hint
Think about how many points fall inside the unit circle compared to total points.
❓ data_output
intermediate
2: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)
A5000
B4000
C3932
D2500
Attempts:
2 left
💡 Hint
Count how many points satisfy the circle condition.
❓ visualization
advanced
3: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()
AScatter plot with points outside circle in blue only
BRed points inside circle, blue points outside, no legend
CAll points in one color, no distinction
DBlue points inside circle, red points outside, circle boundary visible
Attempts:
2 left
💡 Hint
Check colors and labels for inside vs outside points.
🧠 Conceptual
advanced
1: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?
AError decreases proportionally to 1 divided by the square root of the number of points
BError decreases linearly with the number of points
CError increases as more points are added
DError remains constant regardless of the number of points
Attempts:
2 left
💡 Hint
Think about how randomness averages out with more samples.
🔧 Debug
expert
2: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)
ASyntaxError due to missing parentheses
BNo error, prints the count of points inside circle
CTypeError because y is a float, not an array, so x**2 + y**2 fails
DValueError because points is not an integer
Attempts:
2 left
💡 Hint
Check the shape and type of y compared to x.

Practice

(1/5)
1. What is the main purpose of a Monte Carlo simulation in data science?
easy
A. To create visual charts
B. To sort data quickly
C. To estimate results using random sampling
D. To clean missing data

Solution

  1. Step 1: Understand Monte Carlo simulation basics

    Monte Carlo simulation uses random sampling to estimate results when exact calculation is hard.
  2. Step 2: Identify the main purpose

    It helps estimate outcomes by simulating many random scenarios.
  3. Final Answer:

    To estimate results using random sampling -> Option C
  4. Quick Check:

    Monte Carlo = random sampling estimation [OK]
Hint: Monte Carlo = random sampling for estimation [OK]
Common Mistakes:
  • Confusing Monte Carlo with data sorting
  • Thinking it cleans data
  • Assuming it creates charts
2. Which numpy function is commonly used to generate random samples for Monte Carlo simulations?
easy
A. numpy.random.rand()
B. numpy.array()
C. numpy.mean()
D. numpy.sort()

Solution

  1. Step 1: Identify numpy functions for randomness

    numpy.random.rand() generates random numbers between 0 and 1, useful for sampling.
  2. Step 2: Compare with other options

    numpy.array() creates arrays, numpy.mean() calculates average, numpy.sort() sorts data; none generate random samples.
  3. Final Answer:

    numpy.random.rand() -> Option A
  4. Quick Check:

    Random samples = numpy.random.rand() [OK]
Hint: Random samples come from numpy.random.rand() [OK]
Common Mistakes:
  • Choosing numpy.array() instead of random function
  • Confusing mean or sort with random sampling
  • Using functions that don't generate randomness
3. What is the output of this code snippet?
import numpy as np
np.random.seed(0)
samples = np.random.rand(5)
mean_val = np.mean(samples)
print(round(mean_val, 2))
medium
A. 0.50
B. 0.57
C. 0.60
D. 0.45

Solution

  1. Step 1: Generate 5 random samples with seed 0

    Using np.random.seed(0) fixes randomness. np.random.rand(5) produces [0.5488135, 0.71518937, 0.60276338, 0.54488318, 0.4236548].
  2. Step 2: Calculate mean and round

    Sum = 2.83530423, mean = 0.567060846, round(mean_val, 2) = 0.57.
  3. Final Answer:

    0.57 -> Option B
  4. Quick Check:

    Mean of samples = 0.57 [OK]
Hint: Seed fixes random values; mean rounds to 0.57 [OK]
Common Mistakes:
  • Not setting seed causing different outputs
  • Rounding incorrectly
  • Calculating sum or mean wrongly
4. Identify the error in this Monte Carlo simulation code:
import numpy as np
samples = np.random.rand(1000)
result = sum(samples) / 100
print(result)
medium
A. Using np.random.rand() instead of np.random.randint()
B. Not setting random seed
C. Missing np.mean() function
D. Dividing by 100 instead of number of samples

Solution

  1. Step 1: Check calculation of average

    sum(samples) sums 1000 values, but dividing by 100 underestimates average.
  2. Step 2: Correct divisor

    To get average, divide sum by 1000 (number of samples), not 100.
  3. Final Answer:

    Dividing by 100 instead of number of samples -> Option D
  4. Quick Check:

    Average = sum / number of samples [OK]
Hint: Divide sum by sample count, not fixed number [OK]
Common Mistakes:
  • Dividing by wrong number
  • Confusing random functions
  • Ignoring seed importance
5. You want to estimate the probability that a random point inside a 1x1 square falls within a circle of radius 0.5 centered at (0.5, 0.5) using Monte Carlo simulation. Which numpy code correctly estimates this probability?
hard
A. points = np.random.rand(10000, 2) dist = np.sqrt((points[:,0]-0.5)**2 + (points[:,1]-0.5)**2) prob = np.sum(dist <= 0.5) / 10000 print(prob)
B. points = np.random.rand(10000) dist = np.sqrt((points-0.5)**2) prob = np.sum(dist <= 0.5) / 10000 print(prob)
C. points = np.random.rand(2, 10000) dist = np.sqrt((points[:,0]-0.5)**2 + (points[:,1]-0.5)**2) prob = np.sum(dist <= 0.5) / 10000 print(prob)
D. points = np.random.randint(0, 1, (10000, 2)) dist = np.sqrt((points[:,0]-0.5)**2 + (points[:,1]-0.5)**2) prob = np.sum(dist <= 0.5) / 10000 print(prob)

Solution

  1. Step 1: Generate 2D points inside 1x1 square

    np.random.rand(10000, 2) creates 10000 points with x,y in [0,1].
  2. Step 2: Calculate distance from center (0.5,0.5)

    Distance formula sqrt((x-0.5)^2 + (y-0.5)^2) applied to each point.
  3. Step 3: Count points inside circle and estimate probability

    Count points with distance ≤ 0.5, divide by total points for probability.
  4. Final Answer:

    np.random.rand(10000, 2) with column indexing for coordinates -> Option A
  5. Quick Check:

    2D points + distance check = probability [OK]
Hint: Use 2D random points and distance formula [OK]
Common Mistakes:
  • Using 1D points instead of 2D
  • Wrong shape for points array
  • Using randint instead of rand for floats
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard