Assignment
There is a desk in your office that has a 50% chance of holding one euro. This desk has three drawers. If there is a euro in this desk, it is equally likely to be in one of the drawers. What is the probability that the euro is in the third drawer if you have already searched in vain for it in the in the first and second?
Solution:
int iterations = 10000;
int desk;// 0 or 1 - 50%
int[] foundEuro = new int[5];
for (int i=1; i <= iterations; i++){
desk = (int) (Math.random() * 2);
if ( desk == 0){ // found Euro
int drawer = (int) (Math.random() * 3);
if ( drawer == 0){
foundEuro[drawer]++; // euro in drawer1
} else {
foundEuro[drawer]++; // euro in drawer2
foundEuro[drawer+1]++; // euro in drawer3
}
} else {
foundEuro[desk]++;
}
}
showResult(foundEuro);
float probability = ( ((float) foundEuro[0]) / iterations) * 100;
System.out.printf("%.2f%%", probability);
Output
Euro in drawer 1: 1638
Euro in drawer 2: 6622
Euro in drawer 3: 3343
16,38%
Note
I think, my code has no errors and is supposed to show the right result, but idk if it is really the right probability for finding the euro in the third drawer, while in the other first two drawers it wasn't there.
Aucun commentaire:
Enregistrer un commentaire