Skip to content

Instantly share code, notes, and snippets.

@JulianNorton
Created August 25, 2017 23:29
Show Gist options
  • Save JulianNorton/8e09c44cbf6f04d8ba628b488fcba8d9 to your computer and use it in GitHub Desktop.
Save JulianNorton/8e09c44cbf6f04d8ba628b488fcba8d9 to your computer and use it in GitHub Desktop.
# https://fivethirtyeight.com/features/work-a-shift-in-the-riddler-gift-shop/
# riddler express
# https://docs.google.com/spreadsheets/d/1n2WNPOeR2iOeWf5pUpHU6P92MDH7sk3rsUtvdFUsmHM/edit#gid=0
import random
random.seed(0)
# what if the half pills fall out of your hands back into the bottle?
# let p stand for probablity between 0 and 1.
def sample_pills_extra_credit(i, p):
iteration_history = []
for i in range(i):
current_iteration = 0
full_pills = 100
half_pills = 0
ate_half = False
while ate_half == False:
current_iteration += 1
if random.randint(1,100) <= full_pills:
full_pills += -1
half_pills += 1
# print(full_pills, half_pills)
else:
if random.uniform(0,1) < p:
# print('you fumbled a pill back into the bottle, and you reach for another')
current_iteration += -1
else:
# print('You just ate a half pill')
# print('Current iteration =', current_iteration)
iteration_history.append(current_iteration)
break
# print(iteration_history)
average_iterations = sum(iteration_history) / len(iteration_history)
print(p, 'fumble chance', 'average iterations (mean) for', len(iteration_history),'iterations =', average_iterations, '\n')
sample_pills_extra_credit(1000000, 0)
sample_pills_extra_credit(1000000, .1)
sample_pills_extra_credit(1000000, .2)
sample_pills_extra_credit(1000000, .3)
sample_pills_extra_credit(1000000, .4)
sample_pills_extra_credit(1000000, .5)
sample_pills_extra_credit(1000000, .6)
sample_pills_extra_credit(1000000, .7)
sample_pills_extra_credit(1000000, .8)
sample_pills_extra_credit(1000000, .9)
# 0 fumble chance average iterations (mean) for 1000000 iterations = 13.218755
# 0.1 fumble chance average iterations (mean) for 1000000 iterations = 13.81488
# 0.2 fumble chance average iterations (mean) for 1000000 iterations = 14.53912
# 0.3 fumble chance average iterations (mean) for 1000000 iterations = 15.367559
# 0.4 fumble chance average iterations (mean) for 1000000 iterations = 16.42008
# 0.5 fumble chance average iterations (mean) for 1000000 iterations = 17.747487
# 0.6 fumble chance average iterations (mean) for 1000000 iterations = 19.515661
# 0.7 fumble chance average iterations (mean) for 1000000 iterations = 22.02279
# 0.8 fumble chance average iterations (mean) for 1000000 iterations = 26.134048
# 0.9 fumble chance average iterations (mean) for 1000000 iterations = 34.646415
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment