Created
September 16, 2013 13:59
-
-
Save actsasgeek/6581048 to your computer and use it in GitHub Desktop.
The Birthday Problem in Python.
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
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