Skip to content

Instantly share code, notes, and snippets.

@possibly-wrong
Created December 19, 2019 17:08
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 possibly-wrong/2e33f6c137383b896474daa53d72daab to your computer and use it in GitHub Desktop.
Save possibly-wrong/2e33f6c137383b896474daa53d72daab to your computer and use it in GitHub Desktop.
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