Hi
Im very new to java and trying to make a simple java program which will work out the chances of two people sharing the same birthday in say a class of 40 people. Using the monte Carlo method the above senerio will be simulated 10,000 times. Below is the code I have already written
public class birthday { public static void main(String[] args) { long experiments = 10000; long bdaycount = 0; int bday1, bday2; for(long i = 0; i < experiments; i++) { for(int j = 0; j < 20; j++) { bday1 = probability(); bday2 = probability(); if(bday1 == bday2) { bdaycount++; break; } } } System.out.println("prob is: " + ((double)bdaycount / (double)experiments)); } public static int probability() { double x = Math.random(); x = 1.0 + (x * 365); int outcome = (int)Math.floor(x); return outcome; } }
But I quickly figured at that all this is doing is getting to birthdays at a time and comparing those instead of the entire class of 40. So my question is how can I compare the whole class and see if there are 2 or more birthdays in which are the same. I thought that it may be possible to store the values in an array but im not sure.
Any help will be appreciated.
Thanks