Created
October 30, 2015 20:13
-
-
Save allenanie/acf48439b8f452a6a1d1 to your computer and use it in GitHub Desktop.
A Python simulation to Coupon Collector's Problem
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
# simulate coupon collector's problem | |
import numpy as np | |
def run(n): | |
""" | |
n: number of different coupons (stamps) | |
return: days it took to achieve such goal | |
""" | |
days = 0 | |
collected = set() | |
while True: | |
# check if we have enough | |
if len(collected) == n: | |
break | |
# we try to collect stamps | |
got = np.random.randint(0, n) # choose a stamp, randomly 1/n | |
days += 1 | |
collected.add(got) | |
return days | |
if __name__ == '__main__': | |
totaldays = 0 | |
for x in range(1, 1000): | |
totaldays += run(100) | |
print(totaldays / 1000.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment