# Correct logic in Monty Hall Problem? [closed]

I've written this code in an attempt to create the Monty Hall problem and check the ratios. I would like someone to check the logic on it. I know it isn't organized well at all; I just need to see if I'm performing correctly.

private static int losses = 0;
private static int wins = 0;
public static void main(String[] args) throws Exception {
for (int x = 0; x < 100; x++) {
boolean doors[] = getDoors();
run(getGuess(), doors);
}
System.out.println("Wins: " + wins);
System.out.println("Losses: " + losses);
double ratio = wins / wins + losses;
System.out.println("Ratio: " + ratio);
}

public static boolean[] getDoors() {
//door range
int min = 1;
int max = 3;
//generate random correct door
Random r = new Random();
int i = r.nextInt(max - min) + min;
//assign doors to boolean
boolean[] doors = new boolean;
for (int x = 0; x < 3; x++) {
if (x == i) {
doors[x] = true;
} else {
doors[x] = false;
}
}
return doors;
}

public static int getGuess() {
//guess range
int min = 1;
int max = 3;
//generate random guess
Random r = new Random();
int guess = r.nextInt(max - min) + min;
return guess;
}

public static void run(int guess, boolean[] doors) {
//if guess is valid before switch, declare loss
if (doors[guess]) {
losses++;
return;
}
//find other false door
int wrongDoor = 0;
for (int x = 0; x < 3; x++) {
if (doors[x] == false && guess != x) {
wrongDoor = x;
}
}
//switch doors
int newGuess = 0;
for (int x = 0; x < 3; x++) {
if (x != wrongDoor && x!= guess) {
newGuess = x;
}
}
//checkguess
if (doors[newGuess] == true) {
wins++;
} else {
losses++;
}
}


## closed as off-topic by abuzittin gillifirca, ChrisWue, Simon Forsberg, syb0rg, Malachi♦Dec 13 '13 at 14:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Your question must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After getting your code to work, you may edit this question seeking a review of your working code." – abuzittin gillifirca, ChrisWue, Simon Forsberg, syb0rg, Malachi
If this question can be reworded to fit the rules in the help center, please edit the question.

• if (doors[newGuess] == true) is equivalent to if (doors[newGuess]) – David Harkness Dec 13 '13 at 3:58

You know it is not running correctly, the ratio is supposed to be 66%, not 50%.... what you really want is some help debugging it.....

Your issue is in your random number generation.... you are only ever setting, and choosing 2 of the three doors. Have a look at this common problem:

https://stackoverflow.com/questions/363681/generating-random-numbers-in-a-range-with-java

You are not using the ranges properly:

    //door range
int min = 1;
int max = 3;
//generate random correct door
Random r = new Random();
int i = r.nextInt(max - min) + min;


in this code, max-min is 2, and nextInt(2) is going to be either 0, or 1., add that to min you get either 1 or 2.

You should not care about the min and max, you should just use:

int i = r.nextInt(doorcount); // assuming doorcount == 3


which will return 0, 1, or 2

If you fix this in both the getGuess and getDoors method then you will get the correct ratio of 66%.

Your code has a number of other problems too.

1. there is no need to create a new Random instance each time you want a random number. Create a single instance and share it.
2. do not call your method run. This will lead to confusion with the java.lang.Runnable interface
3. the line: double ratio = wins / wins + losses does not do what you think it does.

I think you should fix your program, then resubmit it for review....

• To know the probability that a contestant offered the chance to switch should do so, one must know the probability with which the host will show the empty door [rather than announcing a win] if the player's initial guess is correct, and the probability with which the host will show the empty door [rather than announcing a loss] if the player's initial guess is wrong. If both probabilities are 100%, a player would have a 2/3 chance of winning by switching, but I don't think the real Monty Hall behaved like that. Host behavior can shift the odds of win-by-switching anywhere from 0% to 100%. – supercat Dec 13 '13 at 21:40
• @supercat - the 'Monty-Hall' problem requires the host to reveal one door and then offer the chance to switch. Whether the actual Monty-Hall in his actual show always did this, is not significant when considering the problem named 'Monty-Hall' ;) – rolfl Dec 13 '13 at 21:46