Skip to content

Instantly share code, notes, and snippets.

@itdxer
Created October 4, 2020 14:26
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
import random
n_start_milk_choc = 2
n_start_black_choc = 8
n_trials = 1000000
n_milk_last = 0
for _ in range(n_trials):
prev_pulled_choc = None
n_milk_choc = n_start_milk_choc
n_black_choc = n_start_black_choc
while n_black_choc > 0 and n_milk_choc > 0:
pulled_choc = random.random() < n_black_choc / (n_black_choc + n_milk_choc)
if prev_pulled_choc is None or prev_pulled_choc == pulled_choc:
if pulled_choc == 1:
n_black_choc -= 1
else:
n_milk_choc -= 1
if prev_pulled_choc is None:
prev_pulled_choc = pulled_choc
elif prev_pulled_choc != pulled_choc:
prev_pulled_choc = None
assert n_black_choc != 0 or n_milk_choc != 0
n_milk_last += (n_milk_choc > 0)
print("Fraction of simulations with last milk chocolate", n_milk_last / n_trials)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment