Riddler Express
It’s your 30th birthday (congrats, by the way), and your friends bought you a cake with 30 candles on it. You make a wish and try to blow them out. Every time you blow, you blow out a random number of candles between one and the number that remain, including one and that other number. How many times do you blow before all the candles are extinguished, on average?
Solution
from random import randint
= 30
num_candles = 10000
runs
= 0
total_num for _ in range(runs):
= num_candles
candles = 0
n while(candles):
= randint(1, candles)
blown -= blown
candles += 1
n += n
total_num print("Average number of blows: ", total_num/runs)
The average number of blows based on the simulation above is \(\approx 4\).
Riddler Classic
You and I stumble across a 100-sided die in our local game shop. We know we need to have this die — there is no question about it — but we’re not quite sure what to do with it. So we devise a simple game: We keep rolling our new purchase until one roll shows a number smaller than the one before. Suppose I give you a dollar every time you roll. How much money do you expect to win?
Solution
from random import randint
= 10000
runs
def average_winnings(num_sides, runs = runs):
= 0
total for _ in range(runs):
= randint(1, num_sides)
prev_roll = 1
n while(True):
= randint(1, num_sides)
cur_roll += 1
n if cur_roll < prev_roll:
break
else:
= cur_roll
prev_roll += n
total return total/runs
print("Average winnings: ", average_winnings(100))
The average winnings is \(\approx 2.73\).
Extra Credit
Extra credit: What happens to the amount of money as the number of sides increases?
Solution
The average winnings doesn’t change with the number of sides.