Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA ONLY Using a hash set to do a Monte Carlo analysis of the birthday paradox.

ID: 3777410 • Letter: J

Question

JAVA ONLY

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: 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.

Explanation / Answer

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class MonteCarloBirthday {

   /**
   * @param args
   */
   public static void main(String[] args) {

       Random random = new Random();
       int test_cases = 100000;
       //running the loop from 10 to 100 with gap of 10
       for (int people = 10; people < 100; people += 10) {
           int collisionCount = 0; //collision counter
           float prob = 0; //probability number
           for (int eachCases = 0; eachCases < test_cases; eachCases++) {
               // Constructs a new empty set that has the specified initial
               // capacity
               Set<Integer> birthdaysSet = new HashSet<>(365);
               birthdaysSet.add(random.nextInt(365) + 1);// random number generator from 1 to 365
               for (int i = 0; i < people; i++) {
                   int randomBirthday = random.nextInt(365) + 1;
                   //if the number is already in hashset
                   if (birthdaysSet.contains(randomBirthday)) {
                       collisionCount++;
                       break;
                   }
                   birthdaysSet.add(randomBirthday);
               }
               //calculating probability
               prob = (float) collisionCount / test_cases;

           }
           System.out.println("After " + test_cases + " tests there were "
                   + collisionCount + " occurrences of shared "
                   + " birthdays in a set of " + people + " people.");
           System.out.println("Probability : " + prob);
       }

   }

}

---------------------output-----------------

After 100000 tests there were 13993 occurrences of shared birthdays in a set of 10 people.
Probability : 0.13993
After 100000 tests there were 44403 occurrences of shared birthdays in a set of 20 people.
Probability : 0.44403
After 100000 tests there were 73123 occurrences of shared birthdays in a set of 30 people.
Probability : 0.73123
After 100000 tests there were 90478 occurrences of shared birthdays in a set of 40 people.
Probability : 0.90478
After 100000 tests there were 97544 occurrences of shared birthdays in a set of 50 people.
Probability : 0.97544
After 100000 tests there were 99490 occurrences of shared birthdays in a set of 60 people.
Probability : 0.9949
After 100000 tests there were 99935 occurrences of shared birthdays in a set of 70 people.
Probability : 0.99935
After 100000 tests there were 99988 occurrences of shared birthdays in a set of 80 people.
Probability : 0.99988
After 100000 tests there were 99998 occurrences of shared birthdays in a set of 90 people.
Probability : 0.99998

---------------------output ends-----------------