Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA lang test the Birthday Paradox, which states that if 23 people gather, the chances are 50-50 that there are two people in the group

JAVA lang test the Birthday Paradox, which states that if 23 people gather, the chances are 50-50 that there are two people in the group having the same birthdays. This observation comes from the following analysis. We assume there are 365 days in a year. We also assume that the birthdays of people are evenly distributed on the 365 days; that is, for any day of the year, the proportion of the people born on that day is 1/365 of the entire population. Under these assumptions, the probability that randomly selected 23 people have distinct birthdays from each other is: 365 * 364 * ... * 343 ---------------------- 365^23 In the denominator, there is no constraint about the birthdays; each person picks up one birthday from the 365 possibilities. In the numerator, the 23 people select birthdays one after another, so that no two have the same birthdays; the first person has 365 possibilities, the second has 364 because he/she must not pick the one that the first person has chosen, the third person has 363 because he/she must not pick the ones that the first and the second persons have chosen, and so on. The fraction is about 0.50, so the chances are almost 50-50 that there are two people having the same birthdays. ######################################################################### The centerpiece of the code is a method, oneTrial, that, given a number of people as its parameter, randomly selects birthdays for those people and produces for each day how many people have chosen to have that birthday. More formally, the method has the number of people, nPeople, as its formal parameter and returns an int array. The method first instantiates an array of 365 elements, say theCounts. Then, for nPeople times (using a for-loop), the method selects a random integer between 0 and 364, and then increases the element of theCounts at the selected integer. After that it returns the array theCounts. After receiving the return value of oneTrial, we examine the slots of the array for an entry greater or equal to 2. Such a slot indicates the existence of multiple having the same birthdays. We give this task to a method hasAHit. This method has its formal parameter an integer array and returns a boolean. Using a for-loop, the method scans the slots of the formal parameter. On encountering a slot value >= 2, the method returns true, thereby ignoring the remainder of the code in the method. After the for-loop, the method returns false, indicating that no slot >=2 has been encountered. Using the above two methods, we write another method, experiment1. The method experiment1 has two formal parameters. The first is an int by the name of nPeople, which is the value passed to oneTrial as the number of people. The second is also an int and has the name of nReps, which is the number of times to execute oneTrial. We use a for-loop to count the executions of oneTrial. After each execution, we feed the return array of oneTrial to hasAHit. If hasAHit returns true, we increase the value of a double variable, hitRate, by 1. The initial value of this variable is 0. After finishing the for-loop, we divide hitRate by nReps. The value of the variable then becomes the proportion of the repetitions in which the random birthday selections generated multiple people having the same birthdays. The method experiment1 reports this ratio before terminating. The main method receives the quantities for nPeople and nReps and calls experiment1 with these two values. To print the average, use "%.3f" in printf so that exactly three digits appear after the decimal point. 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions