The paradox states that the odds of 2 people in a group sharing a birthday is su
ID: 3547415 • Letter: T
Question
The paradox states that the odds of 2 people in a group sharing a birthday is surprisingly high.
A Monte Carlo analysis is using random numbers to simulate actual probable outcomes.
Test your code for various numbers of people.
Here is pseudo code of the algorithm:
Set number of collisions to zero
Loop (10 to100 by 10)
Loop:
Generate a birthday. (use 1-365)
See if it is already in the set. If it is count as a collision and end the loop.
Add to the set.
Print out the number of collisions as a probability P.
For example, if you ran 50 people and got 25 collisions P = 0.5
The probability of 100 is around 1.0. P can never exceed 1.0. If you exceed 1.0 you are counting collisions in a group more than once.
Example Output:
--------------------Configuration: <Default>--------------------
After 100000 tests there were 11446 occurrences of shared birthdays in a set of 10 people.
Probability: 0.11446
After 100000 tests there were 41031 occurrences of shared birthdays in a set of 20 people.
Probability: 0.41031
After 100000 tests there were 70455 occurrences of shared birthdays in a set of 30 people.
Probability: 0.70455
After 100000 tests there were 89061 occurrences of shared birthdays in a set of 40 people.
Probability: 0.89061
After 100000 tests there were 97043 occurrences of shared birthdays in a set of 50 people.
Probability: 0.97043
After 100000 tests there were 99454 occurrences of shared birthdays in a set of 60 people.
Probability: 0.99454
After 100000 tests there were 99926 occurrences of shared birthdays in a set of 70 people.
Probability: 0.99926
After 100000 tests there were 99986 occurrences of shared birthdays in a set of 80 people.
Probability: 0.99986
After 100000 tests there were 99999 occurrences of shared birthdays in a set of 90 people.
Probability: 0.99999
After 100000 tests there were 100000 occurrences of shared birthdays in a set of 100 people.
Probability: 1.0
Process completed.
code i need to be fix
import java.util.HashSet;
import java.util.Random;
public class DaughertyBirthday
{
public static void main(String[] args)
{
Random rand = new Random();
HashSet<Integer> birthday = new HashSet<Integer>( );
int runs = 5000;
int collideCount = 0;
for(int j = 0; j < runs; j++) {
int chosen = Math.abs(rand.nextInt()) % 365 + 1;
if( birthday.contains(chosen)) {
collideCount++;
}
birthday.add(chosen);
}
System.out.println(collideCount + " collisions over " + runs + " runs.");
for(int i = 0; i < 51; i++){
float prob = collideCount / 5000;
System.out.println(i + " people equals " + prob + " probability.");
}
}
}
Explanation / Answer
In each experiment, do 5000 trials. In each trial you simply assign to every person (the number of people depends on which experiment you're performing, of course) a random number (1 through 365). Then see if 2 people have the same number. If they do, the trial is a success. Then just count the number of successful trials and divide them by 5000. This will give you the result for one experiment
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.