Published on
August 28, 2023
Written by
Jesse Ching
Category
Python
In a scenario, you’re walking up a building with a friend and play a game where you throw a die 100 times. If it lands on one or two (-1 step), if it lands on three, four or five (+1 step), and if it lands on six, you’ll throw it again to move up various (+1, +2, +3, +4, +5 or +6) steps. In 100 die rolls, you bet your friend that you’ll reach 60 steps high.
To understand what the chances are that you’ll win this bet, simulate the process thousands of times to find the result ≥ 60 — a form of hacker statistics.
Random Number Generators
import numpy as np
np.random.seed(123)
dice = np.random.randint(1,7)
- using the numpy sub-package ‘random’, set the random
seed()
so that results are reproducible and userandint()
to generate random integers with appropriate arguments to simulate a die roll.
Determining Your Next Move
step = 0
if dice <= 2:
step = step - 1
elif dice > 2 and dice < 5:
step = step + 1
else:
step = step = np.random.randint(1,7)
- created the control (if-elif-else statement) construct that simulates the game’s parameters. However, this is one random ‘step’ whereas we essentially need to produce a ‘random walk’ to simulate an entire succession of 100 die rolls.
Random Walk
# numpy imported, seed is set
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)
random_walk.append(step)
- simulated the succession of 100 die rolls (or a random walk) using a for-loop. This rolls the die 100 times with an added
max()
so that one cannot go negative. Each of the 100 rolls will be appended to the list random_walk which will store the data of what step the player is on.
Distribution
# numpy imported, seed is set
all_walks = []
for i in range(500):
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)
random_walk.append(step)
all_walks.append(random_walk)
- now that random_walk is complete, we have one game played. To understand what the chances are that we will win this bet, we simulate the random walk thousands of times. All_walks is a list of lists that we’ll transform into a numpy array to start visualizing the data.
Visualization
import matplotlib.pyplot as plt
np_aw_t = np.transpose(np.array(all_walks))
ends = np_aw_t[-1,:]
plt.hist(ends)
plt.show()
- using the matplotlib sub-package peplos, with one argument, python uses the index to map onto the x-axis and use the values in the list onto y. First, turned all_walks into a numpy array, then transposed it so that it represents the position after one throw for the random walks, and lastly, selected the end points of all random walks.
Calculated the Odds
np.mean(ends >= 60)
- the histogram was created from a numpy array ends that contains 500 integers, each representing the end point of random walks, or a succession of 100 die rolls in a played game. To calculate the chance, number of results ≥ 60 divided by 500. Simply use numpy’s
mean()
to find 0.784 or a 78.4% chance of winning this bet.