Correct logic in Monty Hall Problem? 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.

If you don't know what the Monty Hall problem is.

private static int losses = 0;
private static int wins = 0;
public static void main(String[] args) throws Exception {
    for (int x = 0; x &lt; 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[3];
    for (int x = 0; x &lt; 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 &lt; 3; x++) {
        if (doors[x] == false &amp;&amp; guess != x) {
            wrongDoor = x;
        }
    }
    //switch doors
    int newGuess = 0;
    for (int x = 0; x &lt; 3; x++) {
        if (x != wrongDoor &amp;&amp; x!= guess) {
            newGuess = x;
        }
    }
    //checkguess
    if (doors[newGuess] == true) {
        wins++;
    } else {
        losses++;
    }
}
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.


there is no need to create a new Random instance each time you want a random number. Create a single instance and share it.
do not call your method run. This will lead to confusion with the java.lang.Runnable interface
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....