Skip to content

Instantly share code, notes, and snippets.

@jcchurch
Created October 17, 2011 15:07
Show Gist options
  • Save jcchurch/1292817 to your computer and use it in GitHub Desktop.
Save jcchurch/1292817 to your computer and use it in GitHub Desktop.
Coupon Collector's Problem applied to Birthdays
# This is not the actual Birthday Paradox Problem.
# This is something similar suggested by Taylor Smith.
#
# How many people do you need in a room to have a 50%
# chance of having every possible birthday represented
# within the room?
#
# After typing all of this out, I realized that it's
# the same as the Coupon Collector's Problem.
import random
def main():
# Total possible birthdays
BIRTHDAYS = 365
# Number of trials to perform
TRIALS = 1000
# Total number of people across all trials.
total_people = 0
# Keep track of the number of completed trials
i = 0
while i < TRIALS:
# This is our tally of recorded birthdays
calendar = [0] * BIRTHDAYS
# Total number of unique birthdays for this trial
unique_birthdays = 0
# Number of people in the room
people_this_trial = 0
while unique_birthdays < BIRTHDAYS:
# Allow 1 person in the room. Ask them their birthday.
people_this_trial += 1
my_birthday = random.randint(0, BIRTHDAYS-1)
# Check for a unique birthday. If unique, record it.
if calendar[my_birthday] == 0:
calendar[my_birthday] += 1
unique_birthdays += 1
# Finished with 1 trial. Add the people_this_trial to total_people
total_people += people_this_trial
i += 1
# Finally, calculate the average number of people per trial
# for the solution
average_per_trial = total_people / float(TRIALS)
print average_per_trial
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment