Answer:
The Java code is given below with appropriate comments for explanation
Explanation:
// java code to contradict birth day paradox
import java.util.Random;
public class BirthDayParadox
{
public static void main(String[] args)
{
Random randNum = new Random();
int people = 5;
int[] birth_Day = new int[365+1];
// setting up birthsdays
for (int i = 0; i < birth_Day.length; i++)
birth_Day[i] = i + 1;
int iteration;
// varying number n
while (people <= 100)
{
System.out.println("Number of people: " + people);
// creating new birth day array
int[] newbirth_Day = new int[people];
int count = 0;
iteration = 100000;
while(iteration != 0)
{
count = 0;
for (int i = 0; i < newbirth_Day.length; i++)
{
// generating random birth day
int day = randNum.nextInt(365);
newbirth_Day[i] = birth_Day[day];
}
// check for same birthdays
for (int i = 0; i < newbirth_Day.length; i++)
{
int bday = newbirth_Day[i];
for (int j = i+1; j < newbirth_Day.length; j++)
{
if (bday == newbirth_Day[j])
{
count++;
break;
}
}
}
iteration = iteration - 1;
}
System.out.println("Probability: " + count + "/" + 100000);
System.out.println();
people += 5;
}
}
}