Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Monte Carlo simulation of searching for duplicate packs of Skittles
import numpy as np
def sample():
"""Return number of random Skittles packs opened until first duplicate."""
packs = set()
count = 0
while len(packs) == count:
packs.add(tuple(np.bincount(
np.random.randint(0, 5, np.random.binomial(100, 0.6)))))
count = count + 1
return count
total = 0
num_samples = 0
while True:
total = total + sample()
num_samples = num_samples + 1
if num_samples % 100 == 0:
print('Estimate after {0} samples = {1}'.format(
num_samples, total / num_samples))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment