0
0
NumPydata~30 mins

Monte Carlo simulation basics in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Monte Carlo Simulation Basics
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Divide count_target by 10000 and print the result. The output should be a decimal number close to 0.17.