my code below supposes to generate a random days of year and matches every 2 people who have same birthday which also know as birthday problem , the code works but no as desired
public double simulate(int size, int count) {
Random random = new Random();
double x[] = new double[size];
double matches = 0;
boolean isMatch = false;
random.setSeed(count);
for (int i = 0; i < count; i++) {
for (int j = 0; j < size; j++) {
x[j] = random.nextInt(365);
for (int k = j + 1; k < size; k++) {
if (x[j] == x[k]) {
matches++;
isMatch = true;
break;
}
}
if (isMatch) {
isMatch = false;
break;
}
}
}
return (matches/count)*100;
}
and here is the Expected output result
simulate(number of people,number of simulation)
simulate(5, 10000) output = 2.71
simulate(7, 5000) output = 5.34
simulate(2, 10000) output = 0.27
simulate(9, 10000) output = 9.47
simulate(30, 20000) output = 70.675
simulate(15, 50000) output = 25.576
simulate(35, 50000) output = 81.434
simulate(45, 50000) output = 94.2
and this what Actual output :
simulate(5, 10000) output = 2.54
simulate(7, 5000) output = 5.64
simulate(2, 10000) output = 0.18
simulate(9, 10000) output = 9.05
simulate(30, 20000) output = 68.98
simulate(15, 50000) output = 25.12
simulate(35, 50000) output = 79.90
simulate(45, 50000) output = 92.99
thanks for you time .
Aucun commentaire:
Enregistrer un commentaire