Skip to content

Instantly share code, notes, and snippets.

@lvaughn
Created December 7, 2018 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvaughn/649053734d3a2299af0bca9177832b03 to your computer and use it in GitHub Desktop.
Save lvaughn/649053734d3a2299af0bca9177832b03 to your computer and use it in GitHub Desktop.
#!/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