The birthday paradox claims that if there are 23 people in the same room, then the chance 2 of them to have birthday on the same day is 50%.
The following java program confirms that in praxis. We ran the scenario 100.000 times and we find out that 50.500 as an average come up with the 2 people having the birthday on the same date.
import java.util.Random;
public class BirthdayParadox {
public static void main(String[] args){
int[] birthday = new int[23];
int success = 0;
int count_of_tries = 0;
for(int i = 0; i<100000;i++){
count_of_tries++;
if(Collision(birthday) > 0){
success++;
}
}
System.out.print("success: " + success);
System.out.print("count_of_tries: " + count_of_tries);
System.out.print("percentage: " + (success*100)/count_of_tries + "%");
}
public static int Collision(int birthday[]){
Random rand = new Random();
for(int i = 1; i<birthday.length;i++){
birthday[i] = rand.nextInt(365);
}
int count = 0;
for(int i = 0; i<birthday.length; i++){
for(int j= i+1 ; j<birthday.length; j++){
if (birthday[i] == birthday[j]){
count++;
}
}
}
return count;
}
}
Output:
success: 50754
count_of_tries: 100000
percentage: 50%