Boards Index › Chat rooms – the forum communities › Chat forum three boards › Basic IQ / Maths question › Reply To: Basic IQ / Maths question
Turns out I recalled the solution inncorectly. I wrote a simulation to test the problem, swapping doors idoes mprove your chances of winnining.
Copy the following text into https://www.compilejava.net/ to run the program in your browser
import java.util.Random;
public class MontyHall
{
public static float calculateProbability(int doors, boolean swap)
{
Random rand = new Random();
int repeats = 1000;
int wins = 0;
for(int i = 0; i <= repeats; i++) {
// Place the car behind a random door
int car = rand.nextInt(doors);
// Chose a random door
int choice = rand.nextInt(doors);
// Provide an alternate door at random
int option;
if(choice == car) {
option = rand.nextInt(doors);
}else {
option = car;
}
// Swap doors
if(swap) choice = option;
// Check if the chosen door wins
if(choice == car) ++wins;
}
return ((float) wins / (float) repeats) * 100.f;
}
public static void main(String[] args)
{
int doors = 3;
System.out.println(“Simulating the Monty Hall problem with ” + doors + ” doors”);
System.out.println(“Probability of winning when not swapping is ” + calculateProbability(doors, false) + “%”);
System.out.println(“Probability of winning when swapping is ” + calculateProbability(doors, true) + “%”);
}
}