I’m trying a java code to simulate the famous Monty Hall problem but running the code with 100.000 iterations returns an average of 0.61 instead of 0.66 and I can’t find what’s wrong with my code.
public static double MontyHall(int numTests) {
int passedTests = 0;
for(int i = 0; i < numTests; ++i) {
List<Integer> puertas = new ArrayList<>(Arrays.asList(0,0,0)); //0wrong door, 1right door, -1opened door
SecureRandom random = new SecureRandom();
int rightDoor = random.nextInt(3);
puertas.set(rightDoor, 1);
int choice = random.nextInt(3);
if(rightDoor == choice) {
int aux = random.nextInt(2);
for(int k = 0; k < puertas.size(); ++k) {
if(k != rightDoor && aux == 0) {
puertas.set(k, -1);
}
else if(k != rightDoor) --aux;
}
}
else {
for(int j = 0; j < puertas.size(); ++j) {
if(j != rightDoor && j != choice) puertas.set(j, -1);
}
}
for(int l = 0; l < puertas.size(); ++l) {
if(l != choice && puertas.get(l) != -1) choice = l;
}
if(puertas.get(choice) == 1) ++ passedTests;
}
return (double) passedTests /numTests;
}
I run it with numTests being 100.000 and the outcomes are:
0.61116
0.61065
0.61111
Any help would be appreciated,thank you in advance.
4