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
num_candles = 30
runs = 10000
total_num = 0
for _ in range(runs):
candles = num_candles
n = 0
while(candles):
blown = randint(1, candles)
candles -= blown
n += 1
total_num += n
print("Average number of blows: ", total_num/runs)
The average number of blows based on the simulation above is .
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
runs = 10000
def average_winnings(num_sides, runs = runs):
total = 0
for _ in range(runs):
prev_roll = randint(1, num_sides)
n = 1
while(True):
cur_roll = randint(1, num_sides)
n += 1
if cur_roll < prev_roll:
break
else:
prev_roll = cur_roll
total += n
return total/runs
print("Average winnings: ", average_winnings(100))
The average winnings is .
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.