Skip to content

Instantly share code, notes, and snippets.

@actsasgeek
Created September 16, 2013 13:59
Show Gist options
  • Save actsasgeek/6581048 to your computer and use it in GitHub Desktop.
Save actsasgeek/6581048 to your computer and use it in GitHub Desktop.
The Birthday Problem in Python.
from random import randint
from collections import defaultdict
def tally_k_birthdays( k):
counts = defaultdict( int)
for i in range( 0, k):
birthday = randint( 1, 365)
counts[ birthday] += 1
return counts
def identify_same_birthdays( counts_of_birthdays):
for day in counts_of_birthdays.keys():
if counts_of_birthdays[ day] > 1:
return True
return False
def sample_group( size, times):
match = 0.0
for i in xrange( times):
birthday_count = tally_k_birthdays( size)
if identify_same_birthdays( birthday_count):
match += 1.0
return match / times
print sample_group( 23, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment