# Monty Hall Game Show Problem in Javascript

**URL:** <https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614>\
**Category:** Uncategorized\
**Created:** [June 25, 2023, 3:24pm UTC](https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614 "2023-06-25T15:24:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [June 25, 2023, 3:24pm UTC](https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614/1 "2023-06-25T15:24:36Z")

</div>

The following problem is sometimes called “The Monty Hall Game Show  
Problem.” You are a contestant on a game show and have won a shot at the  
grand prize. Before you are three closed doors. Behind one door is a brand  
new car. Behind the other two doors are consolation prizes. The location of  
the prizes is randomly selected. The game show host asks you to select a door,  
and you pick one. However, before revealing the contents behind your door,  
the game show host reveals one of the other doors with a consolation prize.  
At this point, the game show host asks if you would like to stick with your  
original choice or switch your choice to the other closed door. What choice  
should you make to optimize your chances of winning the car? Does it matter  
whether you stick with your original choice or switch doors?  
Write a simulation program to solve the game show problem. Your program should make 10,000 simulated runs through the problem, randomly  
selecting locations for the prize, and then counting the number of times  
the car was won when sticking with the original choice, and counting  
the number of times the car was won when switching doors. Output the  
estimated probability of winning for both strategies. Be sure that your program exactly simulates the process of selecting the door, revealing one,  
and then switching. Do not make assumptions about the actual solution  
(for example, simply assuming that there is a 1/3 or 1/2 chance of getting  
the prize).  
**Can you tell me what the problem is saying without sharing any code?**

**I don’t get how do I start solving this problem? What are the inputs? And what’s the required output?**

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [June 25, 2023, 6:01pm UTC](https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614/2 "2023-06-25T18:01:17Z")

</div>

```auto
let switchNwin = 0;
let firstChoiceWin = 0;
for (let i = 0; i < 10000; i++) {
  let car = Math.floor(Math.random() * 3);
  console.log("car is behind door" + car);
  let goat1;
  for (let j = 0; j < 3; j++) {
    if (j != car) {
      goat1 = j;
      break;
    }
  }
  console.log("first goat is behind door" + goat1);
  let goat2;
  for (j = 0; j < 3; j++) {
    if (j != goat1 && j != car) {
      goat2 = j;
      break;
    }
  }
  console.log("second goat is behind door" + goat2);
  let choice_of_door = Math.floor(Math.random() * 3);
  console.log("User picked door" + choice_of_door);
  //what to do next?
  wanna_switch = Math.floor(Math.random() * 2);
  switch (wanna_switch) {
    case 0:
      if (choice_of_door === car) {
        firstChoiceWin++;
      }
      break;
    case 1:
      choice_of_door = Math.floor(Math.random() * 3);
      if (choice_of_door === car) {
        switchNwin++;
      }
      break;

    default:
      break;
  }
}

console.log(switchNwin);
console.log(firstChoiceWin);

```

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [June 25, 2023, 6:04pm UTC](https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614/3 "2023-06-25T18:04:08Z")

</div>

This is my try but it’s not working. Can you tell why?

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [June 26, 2023, 2:50pm UTC](https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614/4 "2023-06-26T14:50:06Z")

</div>

```auto
let switchNwin = 0;
let firstChoiceWin = 0;
let user_choosed_a_new_door;
for (let i = 0; i < 10000; i++) {
  let car = Math.floor(Math.random() * 3);
  let user_choosed_a_door = Math.floor(Math.random() * 3);
  for (let j = 0; j < 3; j++) {
    if (j != user_choosed_a_door && j != car) {
      host_reveals_a_door_with_a_goat = j;
      break;
    }
  }
  console.log("car is in door" + car);
  console.log("user choosed a door" + user_choosed_a_door);
  console.log(
    "host reveals a door with a goat door" + host_reveals_a_door_with_a_goat
  );

  wanna_switch = Math.floor(Math.random() * 2);
  switch (wanna_switch) {
    case 0:
      //no switch
      if (user_choosed_a_door === car) {
        firstChoiceWin++;
      }
      break;

    case 1:
      //switch
      for (let j = 0; j < 3; j++) {
        if (j != user_choosed_a_door && j != host_reveals_a_door_with_a_goat) {
          user_choosed_a_new_door = j;
          if (user_choosed_a_new_door === car) {
            switchNwin++;
            break;
          }
        }
      }
      break;

    default:
      break;
  }
}

console.log(switchNwin);
console.log(firstChoiceWin);

```

Monty is solved.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [June 27, 2023, 4:05am UTC](https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614/5 "2023-06-27T04:05:15Z")

</div>

Nicely done! 🙂

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [June 27, 2023, 6:06pm UTC](https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614/6 "2023-06-27T18:06:31Z")

</div>

Means a lot 😃 The best part is I didn’t follow any tutorials to do this…Programming is coming to me these days.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [June 29, 2023, 6:12am UTC](https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614/7 "2023-06-29T06:12:11Z")

</div>

That is great to hear! 🕶

---

<div class="post-metadata">

**Author:** ![Metaorange](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@Metaorange](https://forum.kirupa.com/u/Metaorange)\
**Post date:** [July 19, 2023, 8:15pm UTC](https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614/8 "2023-07-19T20:15:15Z")

</div>

```auto
// Function to simulate the Monty Hall Game Show
function simulateMontyHall(totalGames) {
  let stayWins = 0;
  let switchWins = 0;

  for (let i = 0; i < totalGames; i++) {
    // Generate random door numbers (1, 2, or 3)
    const winningDoor = Math.floor(Math.random() * 3) + 1;
    let chosenDoor = Math.floor(Math.random() * 3) + 1;

    // Reveal a non-winning door
    let revealedDoor;
    do {
      revealedDoor = Math.floor(Math.random() * 3) + 1;
    } while (revealedDoor === winningDoor || revealedDoor === chosenDoor);

    // Switch or stay with the original choice
    const switchChoice = Math.random() < 0.5;
    if (switchChoice) {
      chosenDoor = 6 - chosenDoor - revealedDoor;
    }

    // Check if the chosen door matches the winning door
    if (chosenDoor === winningDoor) {
      if (switchChoice) {
        switchWins++;
      } else {
        stayWins++;
      }
    }
  }

  // Calculate win percentages
  const stayWinPercentage = (stayWins / totalGames) * 100;
  const switchWinPercentage = (switchWins / totalGames) * 100;

  // Output results
  console.log(`Games Played: ${totalGames}`);
  console.log(`Stay Wins: ${stayWins} (${stayWinPercentage.toFixed(2)}%)`);
  console.log(`Switch Wins: ${switchWins} (${switchWinPercentage.toFixed(2)}%)`);
}

// Run the simulation with 100,000 games
simulateMontyHall(100000);

```

In this implementation, the `simulateMontyHall` function simulates the Monty Hall Game Show. The function takes a parameter `totalGames` that determines the number of games to be simulated.

Within the simulation, random door numbers are generated for the winning door, the initially chosen door, and the door to be revealed by the host. The player then decides whether to switch their choice or stay with the original door. Afterward, the function checks if the chosen door matches the winning door and increments the respective win count.

At the end of the simulation, the function calculates and displays the number of wins and win percentages for both staying and switching choices.

You can adjust the `totalGames` parameter to change the number of games you want to simulate. The more games you simulate, the closer the win percentages should align with the theoretical probabilities of the Monty Hall problem.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 25, 2023, 8:18am UTC](https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614/9 "2023-07-25T08:18:27Z")

</div>

@polaryeti - thanks to your thread here, you reminded me to post this larger deep dive about the Monty Hall Problem that I drafted a long time ago but forgot about finishing and publishing it 😛

> **[The Monty Hall Problem](https://www.kirupa.com/data_structures_algorithms/monty_hall_problem.htm)**
>
> The Monty Hall problem is a probability puzzle where a counterintuitive strategy of switching doors teaches people the importance of understanding conditional probabilities and the advantage of updating their initial choices based on new information.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 25, 2023, 9:43pm UTC](https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614/10 "2023-07-25T21:43:42Z")

</div>

And here is the code so simulate the Monty Hall problem for an arbitrary number of doors:

```auto
function montyHallSimulation(doorNum, runs) {
  // Track the results
  var wins = 0;
  var losses = 0;

  // Simulate the game
  for (var i = 0; i < runs; i++) {
    // Initialize the doors
    var doors = new Array(doorNum);
    doors.fill("x");

    let chosenDoor = Math.floor(Math.random() * doors.length);
    let carDoor = Math.floor(Math.random() * doors.length);
    let montyDoor = -1;

    doors[chosenDoor] = "chosen";
    doors[carDoor] = "car";

    // The player guessed correctly with their first pick!
    if (chosenDoor === carDoor) {
      doors[chosenDoor] = "chosen + car";

      // We need to reveal some arbitrary door that isn't the 
      // same one the player has selected
      do {
        montyDoor = Math.floor(Math.random() * doors.length);
        //console.log(`${montyDoor} is Monty's pick, and chosen is ${chosenDoor}`);
      } while (montyDoor == chosenDoor);

      doors[montyDoor] = "monty pick";
    } else {
      // Monty's pick will be the door the car is behind
      montyDoor = carDoor;
      doors[carDoor] = "car + monty pick";
    }

    //
    // Tracking!
    //
    if (chosenDoor !== carDoor) {
      // Switching caused a win
      wins++;
    } else {
      // Switching caused a loss
      losses++;
    }
  }

  console.log(`${wins} are the wins and ${losses} are the losses!`);
  console.log(`Switching resulted in a ${100 * (wins / runs)}% win rate!`);
  console.log("------------------------");
}

montyHallSimulation(20, 10000);

```

This example uses 20 doors as in my example from the earlier article, and the win rate for switching is indeed around 95% 🙂

 ![image](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/b/1/b1910986bf6d6f24430b909a462a653016d97b9b.png)

---

<div class="post-metadata">

**Author:** ![kamrunruba](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kamrunruba/32/26011_2.png) [@kamrunruba](https://forum.kirupa.com/u/kamrunruba)\
**Post date:** [April 23, 2024, 4:56pm UTC](https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614/13 "2024-04-23T16:56:43Z")

</div>

Sure, here’s a simple implementation of the Monty Hall problem in JavaScript:

javascriptCopy code

```auto
function montyHallGame(numSimulations, switchDoor) {
    let wins = 0;
    const numDoors = 3;

    for (let i = 0; i < numSimulations; i++) {
        // Randomly select the door with the prize
        const prizeDoor = Math.floor(Math.random() * numDoors);

        // Randomly select the contestant's initial choice
        const chosenDoor = Math.floor(Math.random() * numDoors);

        // Monty opens a door that doesn't have the prize behind it and wasn't chosen by the contestant
        let openedDoor;
        do {
            openedDoor = Math.floor(Math.random() * numDoors);
        } while (openedDoor === prizeDoor || openedDoor === chosenDoor);

        // If switchDoor is true, switch the contestant's choice
        const finalChoice = switchDoor ? (numDoors - chosenDoor - openedDoor) : chosenDoor;

        // Check if the final choice is the prize door
        if (finalChoice === prizeDoor) {
            wins++;
        }
    }

    return wins / numSimulations;
}

// Example usage:
const numSimulations = 10000;
const switchDoor = true;
const winPercentage = montyHallGame(numSimulations, switchDoor);
console.log(`Win percentage with switching doors: ${winPercentage * 100}%`);

```

In this implementation, the `montyHallGame` function takes two parameters: `numSimulations` (the number of simulations to run) and `switchDoor` (a boolean indicating whether the contestant switches doors after Monty reveals a goat). The function returns the percentage of wins.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [April 23, 2024, 4:59pm UTC](https://forum.kirupa.com/t/monty-hall-game-show-problem-in-javascript/659614/14 "2024-04-23T16:59:39Z")

</div>

@kamrunruba - nicely done! 🙂

If you want more of a challenge, are you up for creating three doors and visualizing this code?
