java - Doing a Monte Carlo Analysis of the Birthday Paradox using a HashSet -


disclaimer : not want answer problem. need guidance.

i want perform monte carlo analysis on infamous birthday paradox (determining probability @ least 2 people in given group share same birthday) using hashset.

now when run this, collisioncount way lower expected be.first, expecting collisioncount group of 10 people 11446 (or probability of 0.11446). time got 100 people, expecting collisioncount 100,000 (with probability of 1.0). instead, every 10 people, collisioncount counts 1 (10 people: 1 collision, 20 people: 2 collisions, 30 people: 3 collisions, etc).

here code have wrote far :

import java.util.hashset; import java.util.random; import java.util.set;   public class birthdayparadox { public static void main(string [] args) {     random rand = new random();       int tests = 100000;     int collisioncount = 0;      for(int people = 10; people <= 100; people += 10)     {         set<integer> birthdays = new hashset<>(365);         birthdays.add(rand.nextint(365));         for(int runs = 0; runs < tests; runs++)         {             int randombirthday = rand.nextint(365);              if(birthdays.contains(randombirthday))             {                 collisioncount++;                 break;             }             birthdays.add(randombirthday);         }         float prob = (float)collisioncount / tests;              system.out.println("after " + tests + " tests there " +                                collisioncount + " occurrences of shared " +                                " birthdays in set of " + people + " people.");             system.out.println("probability : " + prob);     }   }    } 

i guess question : not doing right either of for-loops in order collisioncount count correctly?

i new learning java , new stack overflow community , still learning ropes. help/ advice/ tips appreciated.

your problem appears missing 1 of loops.

notice runs loop broken first collision. means value never more 1.

also, never use people variable inside inner loop except when outputting results.

what need run simulation 100_000 times. way place logic within runs loop checks if people people have birthday collision , iterate collision count.


Popular posts from this blog