#1062514

I made a slight mistake in the progam, where if you initially select the car, it can give you the same door you selected as the option to swap to. I haved fixed it here :

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) {
while(option == 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) + “%”);
}
}