Created
May 27, 2014 16:14
-
-
Save thanthese/2d26cb986e6f577ad966 to your computer and use it in GitHub Desktop.
Birthday Problem, Java
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
package com.github.thanthese; | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.Random; | |
public class Main { | |
private static final int days_in_year = 365; | |
private static final int trials_size = 10000; | |
private static final Random random = new Random(); | |
private static boolean is_match_in_trial(int group_size) { | |
Set<Integer> bdays = new HashSet<>(); | |
for (int i = 0; i < group_size; ++i) { | |
bdays.add(random.nextInt(days_in_year)); | |
} | |
return bdays.size() != group_size; | |
} | |
private static void run_trials(int group_size, int number_of_trials) { | |
int number_of_matches = 0; | |
for (int i = 0; i < number_of_trials; ++i) { | |
if (is_match_in_trial(group_size)) { | |
number_of_matches++; | |
} | |
} | |
int percent = (int) (100.0 * number_of_matches / number_of_trials); | |
System.out.println(String.format( | |
"Found matching birthdays amongst %d people in %d out of %d trials, or %d%% of the time.", | |
group_size, number_of_matches, number_of_trials, percent)); | |
} | |
public static void main(String[] args) { | |
for (int group_size = 10; group_size < 30; ++group_size) { | |
run_trials(group_size, trials_size); | |
} | |
} | |
} | |
// Found matching birthdays amongst 10 people in 1202 out of 10000 trials, or 12% of the time. | |
// Found matching birthdays amongst 11 people in 1434 out of 10000 trials, or 14% of the time. | |
// Found matching birthdays amongst 12 people in 1669 out of 10000 trials, or 16% of the time. | |
// Found matching birthdays amongst 13 people in 1989 out of 10000 trials, or 19% of the time. | |
// Found matching birthdays amongst 14 people in 2276 out of 10000 trials, or 22% of the time. | |
// Found matching birthdays amongst 15 people in 2539 out of 10000 trials, or 25% of the time. | |
// Found matching birthdays amongst 16 people in 2798 out of 10000 trials, or 27% of the time. | |
// Found matching birthdays amongst 17 people in 3166 out of 10000 trials, or 31% of the time. | |
// Found matching birthdays amongst 18 people in 3400 out of 10000 trials, or 34% of the time. | |
// Found matching birthdays amongst 19 people in 3761 out of 10000 trials, or 37% of the time. | |
// Found matching birthdays amongst 20 people in 4035 out of 10000 trials, or 40% of the time. | |
// Found matching birthdays amongst 21 people in 4401 out of 10000 trials, or 44% of the time. | |
// Found matching birthdays amongst 22 people in 4832 out of 10000 trials, or 48% of the time. | |
// Found matching birthdays amongst 23 people in 4923 out of 10000 trials, or 49% of the time. | |
// Found matching birthdays amongst 24 people in 5406 out of 10000 trials, or 54% of the time. | |
// Found matching birthdays amongst 25 people in 5746 out of 10000 trials, or 57% of the time. | |
// Found matching birthdays amongst 26 people in 5968 out of 10000 trials, or 59% of the time. | |
// Found matching birthdays amongst 27 people in 6212 out of 10000 trials, or 62% of the time. | |
// Found matching birthdays amongst 28 people in 6585 out of 10000 trials, or 65% of the time. | |
// Found matching birthdays amongst 29 people in 6782 out of 10000 trials, or 67% of the time. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment