Last active
August 29, 2015 14:01
-
-
Save thanthese/8202a701ecb272da23f7 to your computer and use it in GitHub Desktop.
Birthday Problem
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
import random | |
days_in_year = 365 | |
trials_size = 1000 | |
group_sizes = range(10, 30) | |
def is_match_in_trial(group_size): | |
bdays = [random.randint(1, days_in_year) for _ in range(group_size)] | |
return len(set(bdays)) != group_size | |
def run_trials(group_size, number_of_trials): | |
number_of_matches = len([_ for _ in range(number_of_trials) if is_match_in_trial(group_size)]) | |
percent = int(100.0 * number_of_matches / number_of_trials) | |
print "Found matching birthdays amongst {} people in {} out of {} trials, or {}% of the time.".format( | |
group_size, number_of_matches, number_of_trials, percent) | |
for group_size in group_sizes: | |
run_trials(group_size, trials_size) | |
# Found matching birthdays amongst 10 people in 113 out of 1000 trials, or 11% of the time. | |
# Found matching birthdays amongst 11 people in 146 out of 1000 trials, or 14% of the time. | |
# Found matching birthdays amongst 12 people in 176 out of 1000 trials, or 17% of the time. | |
# Found matching birthdays amongst 13 people in 186 out of 1000 trials, or 18% of the time. | |
# Found matching birthdays amongst 14 people in 228 out of 1000 trials, or 22% of the time. | |
# Found matching birthdays amongst 15 people in 248 out of 1000 trials, or 24% of the time. | |
# Found matching birthdays amongst 16 people in 279 out of 1000 trials, or 27% of the time. | |
# Found matching birthdays amongst 17 people in 329 out of 1000 trials, or 32% of the time. | |
# Found matching birthdays amongst 18 people in 367 out of 1000 trials, or 36% of the time. | |
# Found matching birthdays amongst 19 people in 393 out of 1000 trials, or 39% of the time. | |
# Found matching birthdays amongst 20 people in 376 out of 1000 trials, or 37% of the time. | |
# Found matching birthdays amongst 21 people in 444 out of 1000 trials, or 44% of the time. | |
# Found matching birthdays amongst 22 people in 500 out of 1000 trials, or 50% of the time. | |
# Found matching birthdays amongst 23 people in 493 out of 1000 trials, or 49% of the time. | |
# Found matching birthdays amongst 24 people in 546 out of 1000 trials, or 54% of the time. | |
# Found matching birthdays amongst 25 people in 556 out of 1000 trials, or 55% of the time. | |
# Found matching birthdays amongst 26 people in 613 out of 1000 trials, or 61% of the time. | |
# Found matching birthdays amongst 27 people in 624 out of 1000 trials, or 62% of the time. | |
# Found matching birthdays amongst 28 people in 622 out of 1000 trials, or 62% of the time. | |
# Found matching birthdays amongst 29 people in 699 out of 1000 trials, or 69% of the time. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment