We will use hacker statistics to calculate the chances of winning a bet. Use random number generators, loops, and Matplotlib to gain a competitive edge!Imagine you are walking the Empire State Building to Data Camp Head Quarters and you are playing a game with a friend. In the Empire State Building bet, your next move depends on the number of eyes you throw with the dice. We can perfectly code this with an if-elif-else construct! Falling down means you have to start again from step 0.
Betting: you will reach step 60
What is the chance that you will win this bet?
Con toss
import numpy as np
np.random.seed(123)
coin =np.random.randint(0,2)
if coin == 0:
print('head')
else:
print('tails')
head
dict ={'0':'head', '1':'tails'}
toss= [np.random.randint(0,2) for i in range(100)]
[dict[str(coin)] for coin in [np.random.randint(0,2) for i in range(100)]];
Roll the dice
# Import numpy and set seed
import numpy as np
np.random.seed(123)
# Starting step
step = 50
# Roll the dice
dice = np.random.randint(1,7)
# Finish the control construct
if dice <= 2 :
step = step - 1
elif dice<=5 :
step =step + 1
else :
step = step + np.random.randint(1,7)
# Print out dice and step
print(dice)
print(step)
6 53
Random Walk
import numpy as np
# Initialize random_walk
random_walk=[0]
# Complete the ___
for x in range(100) :
# Set step: last element in random_walk
step=random_walk[-1]
# Roll the dice
dice = np.random.randint(1,7)
# Determine next step
if dice <= 2:
step = step - 1
elif dice <= 5:
step = step + 1
else:
step = step + np.random.randint(1,7)
# append next_step to random_walk
random_walk.append(step)
# Print random_walk
print(random_walk)
[0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0, -1, -2, -3, -4, -3, 2, 1, 0, 1, 0, 1, 2, 3, 4, 5, 4, 5, 4, 5, 6, 7, 8, 7, 11, 12, 11, 12, 11, 12, 13, 14, 15, 16, 17, 18, 21, 22, 23, 24, 29, 30, 34, 35, 34, 35, 36, 35, 36, 37, 39, 40, 41, 40, 39, 40, 41, 40, 39, 40, 41, 43, 42, 41, 42, 41, 42, 43, 44, 46, 45, 46, 47, 48, 49, 50, 49, 48, 49, 48, 49, 50, 49, 52, 53, 54, 55, 54, 55, 56, 60]
How low can you go?
# Initialize random_walk
random_walk = [0]
for x in range(100) :
step = random_walk[-1]
dice = np.random.randint(1,7)
if dice <= 2:
# Replace below: use max to make sure step can't go below 0
step = max(0,step - 1)
elif dice <= 5:
step = step + 1
else:
step = step + np.random.randint(1,7)
random_walk.append(step)
print(random_walk)
[0, 0, 0, 2, 1, 2, 4, 5, 6, 11, 10, 11, 12, 13, 14, 15, 14, 19, 20, 21, 22, 21, 20, 19, 18, 17, 18, 19, 20, 26, 25, 24, 23, 24, 25, 26, 25, 26, 27, 26, 31, 32, 31, 30, 29, 28, 29, 28, 27, 29, 30, 33, 34, 36, 37, 38, 39, 38, 37, 38, 39, 40, 41, 40, 41, 42, 43, 46, 47, 48, 47, 48, 47, 48, 49, 50, 54, 53, 52, 53, 54, 55, 54, 55, 54, 55, 57, 62, 61, 62, 63, 64, 65, 66, 67, 66, 67, 68, 69, 71, 73]
Visualize the walk
# Initialization
random_walk = [0]
for x in range(10) :
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)
# Import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
# Plot random_walk
plt.plot(random_walk)
# Show the plot
plt.show()
Simulate multiple walks
# Initialize all_walks (don't change this line)
all_walks = []
# Simulate random walk 10 times
for i in range(10) :
# Code from before
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)
# Append random_walk to all_walks
all_walks.append(random_walk)
# Print all_walks
print(all_walks)
[[0, 0, 1, 0, 1, 2, 4, 3, 2, 3, 4, 3, 5, 6, 7, 8, 9, 10, 11, 10, 11, 12, 17, 18, 17, 16, 15, 14, 15, 16, 17, 22, 23, 22, 23, 24, 25, 26, 27, 28, 27, 26, 27, 26, 32, 31, 32, 31, 32, 31, 32, 36, 40, 42, 44, 43, 44, 46, 45, 51, 52, 54, 55, 54, 55, 56, 57, 56, 57, 58, 59, 61, 60, 59, 60, 66, 71, 70, 69, 73, 74, 75, 76, 78, 77, 78, 77, 78, 77, 76, 82, 81, 82, 83, 84, 85, 86, 87, 88, 89, 88], [0, 1, 0, 0, 1, 0, 1, 0, 1, 2, 6, 7, 6, 12, 11, 10, 11, 10, 9, 10, 9, 13, 12, 13, 12, 13, 12, 13, 15, 14, 13, 12, 13, 12, 17, 18, 19, 18, 17, 18, 17, 18, 17, 22, 23, 22, 21, 20, 19, 20, 21, 25, 31, 32, 33, 34, 35, 34, 35, 37, 38, 39, 40, 39, 38, 39, 38, 39, 40, 41, 40, 41, 42, 43, 42, 41, 42, 43, 48, 49, 50, 55, 54, 55, 54, 55, 56, 57, 58, 57, 63, 62, 63, 62, 61, 62, 66, 67, 68, 69, 70], [0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 5, 6, 7, 6, 7, 8, 11, 10, 11, 10, 11, 14, 15, 16, 17, 16, 15, 18, 19, 20, 21, 20, 19, 20, 23, 24, 29, 30, 29, 30, 29, 30, 29, 35, 34, 33, 34, 36, 35, 34, 33, 34, 35, 36, 37, 41, 46, 47, 48, 49, 52, 53, 54, 55, 56, 57, 58, 57, 56, 57, 58, 57, 63, 62, 63, 64, 63, 62, 63, 62, 61, 67, 66, 65, 66, 67, 66, 65, 64, 63, 64, 70, 71, 74, 75, 81, 82, 83, 89], [0, 0, 6, 5, 6, 7, 8, 7, 13, 14, 13, 14, 13, 14, 13, 14, 13, 14, 18, 19, 21, 20, 21, 20, 19, 20, 19, 20, 19, 23, 27, 26, 25, 24, 25, 26, 30, 31, 32, 36, 35, 34, 33, 34, 35, 37, 36, 35, 36, 37, 39, 42, 47, 48, 47, 48, 49, 50, 51, 52, 51, 52, 53, 59, 58, 63, 62, 66, 65, 69, 68, 71, 72, 71, 72, 78, 77, 76, 75, 79, 78, 77, 78, 79, 78, 79, 80, 81, 82, 81, 80, 79, 80, 79, 78, 82, 83, 85, 89, 95, 94], [0, 0, 1, 2, 3, 2, 3, 2, 3, 4, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 18, 17, 20, 21, 20, 19, 20, 21, 22, 21, 20, 19, 18, 19, 20, 21, 22, 23, 24, 25, 24, 25, 26, 28, 27, 28, 29, 32, 34, 35, 34, 33, 32, 31, 30, 31, 30, 31, 33, 34, 35, 34, 35, 34, 33, 34, 33, 32, 31, 32, 31, 30, 31, 32, 33, 35, 36, 37, 36, 35, 36, 42, 41, 42, 43, 48, 47, 48, 47, 48, 49, 48, 49, 50, 56, 57, 58, 59, 58], [0, 2, 3, 4, 5, 4, 5, 6, 7, 8, 7, 8, 7, 8, 9, 8, 9, 13, 14, 17, 18, 22, 23, 22, 23, 24, 23, 22, 27, 28, 29, 28, 27, 28, 29, 30, 29, 33, 32, 33, 34, 35, 36, 37, 38, 39, 38, 37, 36, 37, 38, 42, 43, 42, 43, 44, 43, 49, 50, 51, 52, 51, 52, 53, 56, 55, 56, 55, 54, 53, 54, 55, 57, 58, 59, 63, 64, 63, 62, 63, 64, 63, 62, 63, 64, 65, 64, 63, 64, 65, 66, 65, 66, 67, 66, 65, 64, 65, 66, 67, 68], [0, 1, 6, 10, 9, 10, 9, 8, 7, 8, 7, 8, 9, 8, 12, 13, 12, 13, 12, 11, 12, 11, 15, 16, 17, 18, 19, 20, 19, 20, 21, 22, 23, 24, 23, 28, 29, 30, 29, 30, 29, 30, 31, 30, 31, 36, 37, 38, 37, 38, 39, 40, 39, 40, 39, 40, 41, 40, 39, 38, 39, 40, 39, 38, 37, 38, 42, 43, 44, 45, 46, 47, 46, 47, 48, 47, 48, 53, 54, 53, 52, 53, 52, 51, 50, 54, 55, 56, 57, 56, 57, 58, 59, 60, 61, 60, 59, 62, 61, 60, 59], [0, 0, 1, 7, 8, 7, 8, 9, 8, 7, 8, 9, 10, 9, 13, 14, 13, 15, 16, 15, 16, 17, 18, 19, 20, 21, 20, 19, 20, 21, 20, 21, 22, 21, 20, 19, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 31, 32, 33, 34, 35, 36, 35, 34, 40, 41, 42, 41, 40, 39, 43, 44, 48, 47, 53, 54, 55, 59, 60, 59, 58, 59, 60, 61, 62, 61, 67, 68, 67, 71, 72, 71, 72, 71, 77, 83, 84, 83, 84, 85, 86, 87, 88, 87, 90, 89, 91, 92, 98, 97, 98], [0, 1, 0, 0, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 13, 14, 15, 16, 15, 14, 13, 14, 13, 17, 16, 15, 16, 18, 19, 24, 23, 24, 25, 29, 28, 29, 30, 29, 28, 29, 30, 31, 30, 29, 28, 29, 28, 27, 28, 27, 26, 25, 26, 27, 26, 25, 24, 25, 26, 27, 26, 25, 26, 27, 28, 29, 28, 29, 28, 29, 30, 31, 32, 33, 38, 43, 42, 43, 44, 43, 44, 50, 49, 50, 49, 50, 49, 50, 51, 52, 51, 50, 51, 52, 53], [0, 0, 0, 0, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 3, 6, 11, 14, 15, 16, 22, 23, 24, 25, 24, 26, 31, 32, 33, 34, 35, 36, 37, 36, 37, 38, 39, 40, 41, 43, 47, 48, 49, 50, 51, 55, 60, 59, 58, 57, 56, 59, 60, 61, 62, 61, 60, 63, 64, 63, 62, 61, 62, 63, 64, 65, 64, 65, 64, 65, 66, 65, 66, 67, 68, 69, 70, 71, 72, 71, 70, 71, 72, 75, 76, 75, 74, 75, 76, 77, 82, 81, 80, 79, 78, 77, 76, 77, 76, 75]]
Visualize all walks
# initialize and populate all_walks
all_walks = []
for i in range(10) :
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)
# Convert all_walks to Numpy array: np_aw
np_aw=np.array(all_walks)
# Plot np_aw and show
plt.plot(np_aw)
plt.show()
# Clear the figure
plt.clf()
# Transpose np_aw: np_aw_t
np_aw_t=np.transpose(np_aw)
# Plot np_aw_t and show
plt.plot(np_aw_t)
plt.show()
Implement clumsiness
You're a bit clumsy and you have a 0.1% chance of falling down.
# Simulate random walk 250 times
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)
# Implement clumsiness
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))
plt.plot(np_aw_t)
plt.show()
Plot the distribution
# Simulate random walk 500 times
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)
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,:]
# Plot histogram of ends, display plot
plt.hist(ends)
plt.show()
Calculate the odds
Chance =len(ends[ends>=60])/(len(ends))
Chance
0.77