Skip to content

Instantly share code, notes, and snippets.

@allenanie
Created October 30, 2015 20:13
Show Gist options
  • Save allenanie/acf48439b8f452a6a1d1 to your computer and use it in GitHub Desktop.
Save allenanie/acf48439b8f452a6a1d1 to your computer and use it in GitHub Desktop.
A Python simulation to Coupon Collector's Problem
# 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