import java.util.concurrent.ThreadLocalRandom;
@author
public class BirthdayParadox {
public static void main(String[] args) {
int numPeople = 23;
final int trials = 1_000_000;
int matches = 0;
for (int i = 0; i < trials; i++) {
if (sampleHasMatch(numPeople)) {
matches++;
}
}
System.out.printf("Sampling %d birthdays at random.%n", numPeople);
double probability = (double) matches / trials * 100;
System.out.printf("%.2f%% probability of a birthday match. %n", probability);
}
private static boolean sampleHasMatch(int sampleSize) {
ThreadLocalRandom rand = ThreadLocalRandom.current();
int[] days = new int[365];
for (int i = 0; i < sampleSize; i++) {
int j = rand.nextInt(days.length);
days[j]++;
if (days[j] == 2) {
return true;
}
}
return false;
}
}