Created
November 5, 2016 05:29
-
-
Save evanlong/4a8898f3e49973e63af57c6a3ebe996b to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
""" | |
Given n people what's the probability that there is a | |
birthday on each day of the year (assuming equal prob | |
for each day & no leap year)? | |
""" | |
import random | |
rand = random.SystemRandom() | |
def assign_n(n): | |
global rand | |
DAYS = 365 | |
day_indexes = set(range(DAYS)) | |
for i in xrange(n): | |
index = rand.randrange(0, DAYS) | |
day_indexes.discard(index) | |
if len(day_indexes) == 0: | |
return True | |
return False | |
def main(): | |
TRIALS_PER_N = 200 | |
for N in xrange(2000, 2500): | |
positive_trials = 0 | |
for trial_index in xrange(TRIALS_PER_N): | |
passed = assign_n(N) | |
if passed: | |
positive_trials += 1 | |
percentage = positive_trials / float(TRIALS_PER_N) | |
print 'N: %d, prob: %f' % (N, percentage) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment