Skip to content

Instantly share code, notes, and snippets.

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
You can’t perform that action at this time.