Skip to content

Instantly share code, notes, and snippets.

@JulianNorton
Last active March 31, 2017 18:10
Show Gist options
  • Save JulianNorton/485e4379a013c24ef81cbaf683d60073 to your computer and use it in GitHub Desktop.
Save JulianNorton/485e4379a013c24ef81cbaf683d60073 to your computer and use it in GitHub Desktop.
If we both arrive at the fountain at an independently random time between noon and 1, what are the chances our picnic actually happens?
# https://fivethirtyeight.com/features/what-are-the-chances-well-meet-for-lunch/
# On a lovely spring day, you and I agree to meet for a lunch picnic at the fountain in the center of our favorite park.
# We agree that we’ll each arrive sometime from noon and 1 p.m.,
# and that whoever arrives first will wait up to 15 minutes for the other.
# If the other person doesn’t show by then, the first person will abandon the plans and spend the day with a more punctual friend.
# If we both arrive at the fountain at an independently random time between noon and 1, what are the chances our picnic actually happens?
import random
# Seed makes the result identical for every run
# random.seed(1)
# 60 sided die
# 12:00 – 12:59
def d60():
result = random.randint(0, 59)
return result
wait_time = 15
# This is where we'll save the output
simluation_result = list()
# Simluation for lunch dates
def lunch_date(iterations):
for x in range(iterations):
# By default, no lunch happens
lunchtime = 0
# Person A at random time
alex_arrives = d60()
# Person B at random time
beth_arrives = d60()
# Did they arrive at the sametime?
if alex_arrives == beth_arrives:
# debug_times()
# print('wow same time!')
# print('Lunchtime!!!!!!')
lunchtime = 1
# Does Alex arrive first?
elif alex_arrives < beth_arrives:
# debug_times()
if alex_arrives + wait_time >= beth_arrives:
lunchtime = 1
else:
pass
# If Alex didn't arrive first, logically Beth will have arrived first.
elif beth_arrives < alex_arrives:
# debug_times()
if beth_arrives + wait_time >= alex_arrives:
lunchtime = 1
else:
pass
simluation_result.append(lunchtime)
# What times did both people arrive?
def debug_times():
print('Alex got here at', '12:', alex_arrives)
print('Beth got here at', '12:', beth_arrives)
# 1 million iterations!
iterations = 1000000
# lunch_date(iterations)
for i in range(10):
lunch_date(iterations)
print(100 * simluation_result.count(1) / iterations)
simluation_result = list()
@agoloprem
Copy link

agoloprem commented Mar 31, 2017

Instead of count(1) you can use reduce.
In this case, they're equivalent. But if the list is like, [1, 3, 5, 2, 1, 99], then reduce would become the clearly correct choice.

EDIT: Python also has a built-in function called sum: http://stackoverflow.com/questions/13909052/how-do-i-add-together-integers-in-a-list-in-python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment