Created
December 19, 2019 17:08
-
-
Save possibly-wrong/2e33f6c137383b896474daa53d72daab to your computer and use it in GitHub Desktop.
Monte Carlo simulation of searching for duplicate packs of Skittles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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