Created
December 7, 2018 15:37
-
-
Save lvaughn/649053734d3a2299af0bca9177832b03 to your computer and use it in GitHub Desktop.
My answer to 538's riddler at https://fivethirtyeight.com/features/the-little-mathematically-determined-house-on-the-prairie/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import random | |
TRIES = 10000000 | |
MORNING_RAIN_PROB = .5 | |
EVENING_RAIN_PROB = .4 | |
def is_raining(prob): | |
return random.random() < prob | |
def simulate_week(): | |
home_count = 2 | |
work_count = 1 | |
for i in range(5): | |
# Morning | |
if is_raining(MORNING_RAIN_PROB): | |
if home_count == 0: | |
return False | |
home_count -= 1 | |
work_count += 1 | |
# Afternoon | |
if is_raining(EVENING_RAIN_PROB): | |
if work_count == 0: | |
return False | |
work_count -= 1 | |
home_count += 1 | |
return True | |
n_dry = 0 | |
for i in range(TRIES): | |
if simulate_week(): | |
n_dry += 1 | |
print("{} tries, {} stayed dry".format(TRIES, n_dry/TRIES)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment