Looking for duplicate birthdays program
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
Code :
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
Re: Looking for duplicate birthdays program
You were right, it would be best to create the birthdays in an array, probably by using another method that creates an int array size 40, and cycles through each index and gives it probability, then calculate from there in either another method or inside the main loop in the main method.
Re: Looking for duplicate birthdays program
Ok ive now created an array and also a loop which gives my my 40 different birthday as showed below.
Code :
int myarray[] = new int[40];
for(int i = 0; i < myarray.length; i++){
myarray[i] = (int)(Math.random() * 365);
}
But now I dont really no how I can check the array for 2 or more of the same values.
Re: Looking for duplicate birthdays program
Re: Looking for duplicate birthdays program
Can I not look in other places for help? I'm just trying to get my coding issue resolved quickly.
If I'm not meant to post on different forums with my questions I apologise.
Is anybody able to help my further with my initial problem?
Cheers
Re: Looking for duplicate birthdays program
Quote:
Originally Posted by
Tronez
Can I not look in other places for help? I'm just trying to get my coding issue resolved quickly.
If I'm not meant to post on different forums with my questions I apologise.
Recommended reading: http://www.javaprogrammingforums.com...s-posting.html
Basically, we don't want to waste our time answering questions that might have already been answered elsewhere. We don't know what kind of advice or information has been exchanged already, so why bother? We have hundreds of posts to get to, so why answer the one that might already be solved somewhere else? The least you can do is provide a link between crossposts so we know what has already been discussed.
Re: Looking for duplicate birthdays program
Sorry about that i genuinely didnt realise, but it wont happen again.
Relating back to the issue with my current code is anybody able to help and give me some advice :)
Thanks
Re: Looking for duplicate birthdays program
I'd be very grateful if somebody could give me a hand with my coding issue.
Ive been trying to do this for a while and still cant get it to work.
Thanks
Re: Looking for duplicate birthdays program
You could use another array of the same size and use it as a 'bucket' and just add to the appropriate bucket each time you check a number