Question
Perform 50,000 experiments with 365 days per year and vary the number of people from 2 to 100. 50,000 runs with 365 days, and 2
Perform 50,000 experiments with 365 days per year and vary the number of people from 2 to 100. 50,000 runs with 365 days, and 2 people, 50,000 runs with 365 days and 3 people, ... 50,000 runs with 365 days and 100 people. Total of 4,950,000 runs, 50,000 runs per experiments * 99 experiments = 4,950,000 runs. For each of the given number of people determine the percentage of experiments where at least one pair of people shared a birthday. At what number of people (between 2 and 100) does the percentage first exceed 50%? Does the answer surprise you? How did it compare to your predicted answer?
Include a table in a comment in your CodeCampTester.java program with the results of this experiment using the following format:
Num people: 2, number of experiments with one or more shared birthday: 120, percentage: 0.24
.....
Num people: 100, number of experiments with one or more shared birthday: 50000, percentage: 100.00
At the top of the table state how many people it requires to have a 50% chance of there being at least 1 shared birthday, given a 365 day year.
Sorry I am so dumb. I already wrote out the code, but I still can't understand how to get the result of this experiment. : (
public static int sharedBirthdays(int numPeople, int numDaysInYear) {
// check preconditions
if (numPeople <= 0 || numDaysInYear <= 0) {
throw new IllegalArgumentException("Violation of precondition: " +
"sharedBirthdays. both parameters must be greater than 0. " +
"numPeople: " + numPeople +
", numDaysInYear: " + numDaysInYear);
}
int[] birthdays = new int[numPeople];
Random random = new Random();
if (numPeople == 1) {
return 0;
} else {
for (int i = 0; i < numPeople; i++) {
birthdays[i] = random.nextInt(numDaysInYear);
}
int numPairs = 0;
for (int i = 0; i < numPeople; i++) {
for (int j = i + 1; j < numPeople; j++) {
if (birthdays[i] == birthdays[j]) {
numPairs++;
}
}
}
return numPairs;
}
}
public static void averagePairs(int numPeople, int numDaysInYear, int trails) {
int pairs = 0;
for (int i = 0; i < trails; i++) {
pairs += sharedBirthdays(numPeople,numDaysInYear);
}
double average = (double) pairs / (trails * numPeople);
System.out.printf("The average number of pairs of people with shared birthdays is %.12f"
, average);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
You want to run 50000 trials for each group size from 2 to 100 and record the percentage of trials where there is at least one pair of people with sha...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started