Everyone must have heard of the famous "Birthday Problem" (Can refer to Wikipedia). I need to write a method (type of double) for it in Java which takes 2 parameters as the "size" and "count". I got a code from somewhere but it does not work on the grader.
public double calculate(int size, int count)
I need to get this method working.
I have tried the following code, but no success.
public double calculate(int size, int count){
double x[] = new double[size];
int match = 0;
for(int i=0;i<count;i++){
for(int j=0;j<size;j++){
x[j] = (int)(Math.random()*365)+1;
}
for(int j=0;j<size;j++){
for(int k=j+1;k<size;k++){
if(x[j]==x[k]){
match++;
}
}}}
double prob = ((double)match/count)*100;
return prob;
}
As I am new student of CS, detailed explanation would be highly appreciated. (This is for my android app development course)
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
I am a beginner, too, also I'm not sure what a grader is so don't expect too much
What I'm missing from the code is the main method calling the calculate method, and the whole code should be inside a public class, e.g.:
Interesting observation.. It would be helpful if @itsabk can link the associated wiki page here
For others: Birthday Problem aims to calculate the probability that two people in the room have same birthday, maximum being 365 people (because at 366, assuming each one has separate birthdays, we will still have at least one pair of matching birthdays).
Have marked the purpose of each statement.
@itsabk Please mention exact nature of your problem. Is your "grader" an automated code evaluation engine? If so, send screenshot of it.
// 1ST INNER LOOP - To add random numbers into the array each indicating
// that a person was born on this day
**// 2ND INNER LOOP - this method will check if any paid of random numbers are
// matching with each other **
**// THIS statement calculates the probability in percentage **