Monty Hall Simulation

by Richard White

A description and analysis of the Monty Hall Problem can be seen here:

I created the following simulation which independently confirms the results as reported in the above wikipedia article.

Each click of the button plays the game a million times.

Totals for each door: Door 1 Door 2 Door 3
Had the prize: 000000 000000 000000
Contestant's initial choice: 000000 000000 000000
Was opened: 000000 000000 000000
Offered for switching: 000000 000000 000000
Results: Won: Lost:
Never switched: 000000 000000
Always switched: 000000 000000

Below is the javascript code for the simulation. Each click of the "Run Simulation" button above executes the function below called "run_simulation".

[Note to programmers: The coding style was chosen with the hope it will be more readable for non-programmers and thus does not include the usual optimizations.]

//__________________________________________________________________________
// define global game variables

    var maxgames = 1000000;  // number of games to play

    var prize         = 0;   // which door the prize is behind
    var choice        = 0;   // which door is chosen initially
    var opendoor      = 0;   // which door gets opened
    var switch_choice = 0;   // which door is offered to switch to

    // how many times the prize was behind this door
    var tally_prize_behind_door_1 = 0;
    var tally_prize_behind_door_2 = 0;
    var tally_prize_behind_door_3 = 0;

    // how many times the contestant chose this door
    var tally_initial_choice_door_1 = 0;
    var tally_initial_choice_door_2 = 0;
    var tally_initial_choice_door_3 = 0;

    // how many times this door was opened
    var tally_opened_door_1 = 0;
    var tally_opened_door_2 = 0;
    var tally_opened_door_3 = 0;

    // how many times this door was offered for switching
    var tally_switched_to_door_1 = 0;
    var tally_switched_to_door_2 = 0;
    var tally_switched_to_door_3 = 0;

    // how many wins and losses
    var never_switch_wins    = 0;
    var never_switch_losses  = 0;
    var always_switch_wins   = 0;
    var always_switch_losses = 0;


//__________________________________________________________________________
function run_simulation() {

    reset_variables();

    // play the game a bunch of times

    for (var game = 0; game < maxgames; game++) {

        // Step 1. Randomly assign the prize to a door number
        prize = getrandom(3);

        // Step 2. Contestant randomly chooses a door number
        choice = getrandom(3);

        // Step 3. Determine which door to open
        opendoor = determine_which_door_to_open();

        // Step 4. Determine which door to offer contestant to switch to
        switch_choice = determine_which_door_for_switching();

        // Step 5. Tally results
        tally_results();

    }

    // After all games played, display the results
    display_results();

}


//__________________________________________________________________________
function reset_variables() {

    prize          = 0;
    choice         = 0;
    opendoor       = 0;
    switch_choice  = 0;

    tally_prize_behind_door_1 = 0;
    tally_prize_behind_door_2 = 0;
    tally_prize_behind_door_3 = 0;

    tally_initial_choice_door_1 = 0;
    tally_initial_choice_door_2 = 0;
    tally_initial_choice_door_3 = 0;

    tally_opened_door_1 = 0;
    tally_opened_door_2 = 0;
    tally_opened_door_3 = 0;

    tally_switched_to_door_1 = 0;
    tally_switched_to_door_2 = 0;
    tally_switched_to_door_3 = 0;

    never_switch_wins    = 0;
    never_switch_losses  = 0;
    always_switch_wins   = 0;
    always_switch_losses = 0;

}


//__________________________________________________________________________
function getrandom(n) {
    // return a random number between 1 and n, inclusive.
    return Math.floor((Math.random() * n) + 1);
}


//__________________________________________________________________________
function pick_randomly(a, b) {
    x = getrandom(2);
    if (x == 1) return a;
    if (x == 2) return b;
}


//__________________________________________________________________________
function determine_which_door_to_open() {

    if (choice == 1) {
        if (prize == 1) return pick_randomly(2, 3);
        if (prize == 2) return 3;
        if (prize == 3) return 2;
    }
    if (choice == 2) {
        if (prize == 1) return 3;
        if (prize == 2) return pick_randomly(1, 3);
        if (prize == 3) return 1;
    }
    if (choice == 3) {
        if (prize == 1) return 2;
        if (prize == 2) return 1;
        if (prize == 3) return pick_randomly(1, 2);
    }
}


//__________________________________________________________________________
function determine_which_door_for_switching() {

    if (choice == 1) {
        if (opendoor == 2) return 3;
        if (opendoor == 3) return 2;
    }
    if (choice == 2) {
        if (opendoor == 1) return 3;
        if (opendoor == 3) return 1;
    }
    if (choice == 3) {
        if (opendoor == 1) return 2;
        if (opendoor == 2) return 1;
    }
}


//__________________________________________________________________________
function tally_results() {

    // Tally the results (add one to the appropriate tally variable).

    // keep track of how many times the prize was behind each door
    if (prize == 1) tally_prize_behind_door_1++;
    if (prize == 2) tally_prize_behind_door_2++;
    if (prize == 3) tally_prize_behind_door_3++;

    // keep track of how many times the contestant chose each door
    if (choice == 1) tally_initial_choice_door_1++;
    if (choice == 2) tally_initial_choice_door_2++;
    if (choice == 3) tally_initial_choice_door_3++;

    // keep track of how many times each door was opened
    if (opendoor == 1) tally_opened_door_1++;
    if (opendoor == 2) tally_opened_door_2++;
    if (opendoor == 3) tally_opened_door_3++;

    // keep track of how many times each door was offered to switch to
    if (switch_choice == 1) tally_switched_to_door_1++;
    if (switch_choice == 2) tally_switched_to_door_2++;
    if (switch_choice == 3) tally_switched_to_door_3++;

    // keep track of wins if contestant never switches
    if (choice == prize) never_switch_wins++;
    if (choice != prize) never_switch_losses++;

    // keep track of wins if contestant always switches
    if (switch_choice == prize) always_switch_wins++;
    if (switch_choice != prize) always_switch_losses++;

}


//__________________________________________________________________________
function display_results() {

    document.getElementById("dr1").innerHTML = tally_prize_behind_door_1;
    document.getElementById("dr2").innerHTML = tally_prize_behind_door_2;
    document.getElementById("dr3").innerHTML = tally_prize_behind_door_3;

    document.getElementById("ch1").innerHTML = tally_initial_choice_door_1;
    document.getElementById("ch2").innerHTML = tally_initial_choice_door_2;
    document.getElementById("ch3").innerHTML = tally_initial_choice_door_3;

    document.getElementById("op1").innerHTML = tally_opened_door_1;
    document.getElementById("op2").innerHTML = tally_opened_door_2;
    document.getElementById("op3").innerHTML = tally_opened_door_3;

    document.getElementById("sw1").innerHTML = tally_switched_to_door_1;
    document.getElementById("sw2").innerHTML = tally_switched_to_door_2;
    document.getElementById("sw3").innerHTML = tally_switched_to_door_3;

    document.getElementById("nsw").innerHTML = never_switch_wins;
    document.getElementById("nsl").innerHTML = never_switch_losses;
    document.getElementById("asw").innerHTML = always_switch_wins;
    document.getElementById("asl").innerHTML = always_switch_losses;

}