JAVA: Please help?! Stuck with birthday paradox! Using a hash set to do a Monte
ID: 3767036 • Letter: J
Question
JAVA: Please help?! Stuck with birthday paradox!
Using a hash set to do a Monte Carlo analysis of the birthday paradox. 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: 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.Explanation / Answer
public static void main(String[] args)
{
// Do 100,000 tests on 10 persons.
System.out.println(birthDayExperiment(10, 100000));
}
public static float birthDayExperiment(int person, int test)
{
int count = 0;
int j = test;
while(--j >= 0) {
Random random = new Random();
Set<Integer> birthdays = new HashSet<>(365);
for(int i = 0; i < person; i++) {
int tmp = random.nextInt(365);
if (birthdays.contains(tmp)) { count++; break; }
birthdays.add(tmp);
}
}
return (float)count / (float)test;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.