Calculate Odds of Winning a Bet using Python Random Number Generation Simulation

1. Project Brief & Specifications
Imagine you are walking up the stairs with a friend at the Q1 Tower in Queensland, Australia. You are playing a game with your friend where you throw a dice 100 times. If the dice roll is a 1 or 2, you will go down one step. If it is 3, 4, or 5, you will go up 1 step. If you roll a 6, you roll the dice again then walk up the resulting number of steps.

It’s obvious, you can not go lower than step number 0. And also, you are a bit clumsy thus have a 0.1% chance of falling down the stairs when you make a move. Falling down means you have to start again from step 0.

You bet your friend that you’ll reach at least 60 steps up after 100 dice rolls. What is the percentage chance that you will win the bet ?

2. Approach & Tools
This project utilizes Python packages Numpy and Matplotlib. It utilizes random number generation simulations to calculate the percentage odds of reaching a specified end point given the specifications in the project brief.

3. Code

# import numpy, matplotlib
import numpy as np
import matplotlib.pyplot as plt

# set seed
np.random.seed(123)

# Simulate random walk 10,000 times
all_walks = []
for i in range(10000) :
    random_walk = [0]
    for x in range(100) :
        step = random_walk[-1]
        dice = np.random.randint(1,7)
        if dice <= 2:
            step = max(0, step - 1)
        elif dice <= 5:
            step = step + 1
        else:
            step = step + np.random.randint(1,7)
        if np.random.rand() <= 0.001 :
            step = 0
        random_walk.append(step)
    all_walks.append(random_walk)

# Create and plot np_aw_t
np_aw_t = np.transpose(np.array(all_walks))

# Select last row from np_aw_t: ends
ends = np_aw_t[-1]

# clear plot
plt.clf()

# labels & tiles
plt.xlabel("Step Reached")
plt.ylabel("Occurences")
plt.title("Reaching 60th step")

# Plot histogram of ends, display plot
plt.hist(ends)
plt.show()

# calculate % chance of reaching >=60 steps
odds = ends >= 60
print(np.mean(odds))

4. Data Visualizations

5. Project Findings
The simulation was run 10,000 times. To determine the % chance of reaching at least 60 steps high after 100 dice rolls, we can create a variable to calculate this then print it:

odds = ends >= 60
print(np.mean(odds))

.7814 = 78.14%

There is a 78.1% chance one can reach at least the 60th step after 100 dice rolls given the conditions specified. The conclusion is that someone betting they could reach at least the 60th step would have a pretty high chance of a successfully winning the bet.

To view the Git repository for this project, click the link below to visit my Git Hub profile.

Random Number Generation to Calculate Odds of Winning a Bet


.