Python Puzzles
The casino has an interesting new game.
You start with a bankroll of $100 and place a $1 bet on a number 1 through 6. You then roll three dice, and if any of the dice rolls match your original bet, you win the number of matching dice ($1, $2, or $3) plus your original $1 bet back. If you lose, your bankroll goes down $1. You must play the game 100 times and keep the sum of your bankroll at the end of the 100 dice rolls.
What are the odds of winning?
Taking the bet is a personal choice, but the odds are not in the bettor’s favor, as you have more to lose than possibly gain.
On average, you will lose $8, but you could lose up to $50 or possibly gain $36.
The average is 92.2649
The percentage over 100 is 0.2243
The min result is 50
The max result is 135
import random
import numpy as np
import matplotlib.pyplot as plt
result = []
for _ in range(10_000):
# start with 100 dollars
i = 100
win = 0
for _ in range(100):
# bet randomly between 0 and 5 dollars
bet = random.randint(0,5)
# roll 3 dice with 6 sides each
diceroll = np.random.choice(6, 3, replace=True)
# you can win 1,2 or 3 points
for x in diceroll:
if bet == x:
win = 1
i += 1
# bettor gets their original point back, or loses their bet
if win == 0:
i -= 1
win = 0
result.append(i)
over100 = 0
for j in result:
if j > 100: over100 += 1
# print the results
print('The average is ' + str(sum(result)/len(result)))
print('The percentage over 100 is ' + str(over100/10_000))
print('The min result is ' + str(min(result)))
print('The max result is ' + str(max(result)))
# plot the histogram
plt.hist(result, bins=20)
plt.xlabel('Amount')
plt.ylabel('Count')
plt.title('Simulation of a Betting Game')
plt.savefig('three-dice-rolls-hist.png', dpi=300, bbox_inches='tight')
plt.show()
