📖 Scenario: Imagine you want to estimate the chance of rolling a total of 7 with two dice. Instead of calculating all possibilities, you can simulate rolling dice many times and see how often you get 7.
🎯 Goal: You will build a simple Monte Carlo simulation using numpy to estimate the probability of rolling a sum of 7 with two dice.
📋 What You'll Learn
Use numpy to generate random dice rolls
Simulate rolling two dice many times
Count how many times the sum is exactly 7
Calculate the estimated probability
Print the final probability
💡 Why This Matters
🌍 Real World
Monte Carlo simulations are used in finance, science, and engineering to estimate probabilities and risks when exact math is difficult.
💼 Career
Data scientists use Monte Carlo methods to model uncertainty and make predictions based on random sampling.
Progress0 / 4 steps
1
Create dice roll simulation data
Import numpy as np and create a variable called rolls that simulates rolling two dice 10000 times using np.random.randint(1, 7, size=(10000, 2)).
NumPy
Hint
Use np.random.randint to generate random integers from 1 to 6 for two dice, 10000 times.
2
Set the target sum for the dice
Create a variable called target_sum and set it to 7 to represent the sum we want to check for.
NumPy
Hint
Just assign the number 7 to a variable named target_sum.
3
Calculate how many rolls sum to the target
Create a variable called count_target that counts how many rows in rolls have a sum equal to target_sum. Use np.sum and boolean indexing with rolls.sum(axis=1) == target_sum.
NumPy
Hint
Use rolls.sum(axis=1) to get sums of each roll, then compare to target_sum, then sum the True values.
4
Calculate and print the estimated probability
Calculate the estimated probability by dividing count_target by the total number of rolls (10000). Store it in probability. Then print the probability with print(probability).
NumPy
Hint
Divide count_target by 10000 and print the result. The output should be a decimal number close to 0.17.
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
Step 1: Understand Monte Carlo simulation basics
Monte Carlo simulation uses random sampling to estimate results when exact calculation is hard.
Step 2: Identify the main purpose
It helps estimate outcomes by simulating many random scenarios.
Final Answer:
To estimate results using random sampling -> Option C
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
Step 1: Identify numpy functions for randomness
numpy.random.rand() generates random numbers between 0 and 1, useful for sampling.
Sum = 2.83530423, mean = 0.567060846, round(mean_val, 2) = 0.57.
Final Answer:
0.57 -> Option B
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
Step 1: Check calculation of average
sum(samples) sums 1000 values, but dividing by 100 underestimates average.
Step 2: Correct divisor
To get average, divide sum by 1000 (number of samples), not 100.
Final Answer:
Dividing by 100 instead of number of samples -> Option D
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?