# Monty Hall Game Show Problem in Javascript

• Comp Sci
• shivajikobardan
In summary: If the car was chosen, keep the first choice if (choice_of_door == car) { firstChoiceWin++; } }console.log(switchNwin);console.log(firstChoiceWin);In summary, the problem is to choose a door, in a three-door box, such that the probability of winning the car is maximized.
shivajikobardan
Homework Statement
Monty Hall Game Show Problem in Javascript
Relevant Equations
none
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).d
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?

Do you understand the game?

PeroK said:
Do you understand the game?
yes. there are 3 doors. one expensive car. two goats. you choose one place. then show host shows you a goat asking you to change the decision.

shivajikobardan said:
yes. there are 3 doors. one expensive car. two goats. you choose one place. then show host shows you a goat asking you to change the decision.
You have to simulate the game 10,000 times. Then estimate the probability of winning the car if you stick with your original choice of door; and, the probability of winning the car if you switch to the remaining door.

JavaScript:
let switchNwin = 0;
let firstChoiceWin = 0;
for (let i = 0; i < 10000; i++) {
let car = Math.floor(Math.random() * 3);
let goat = Math.floor(Math.random() * 3);
let choice_of_door = Math.floor(Math.random() * 3);
let switched_door;
for (j = 0; j < 3; j++) {
switched_door = (j != choice_of_door) && (j != goat) ? j : -1;
}
if (car == switched_door) {
switchNwin++;
}
if (car == choice_of_door) {
firstChoiceWin++;
}
}
console.log(switchNwin);
console.log(firstChoiceWin);
My code is producing wrong output. It should be 66% probability of winning. Why's it not coming that?

shivajikobardan said:
JavaScript:
let switchNwin = 0;
let firstChoiceWin = 0;
for (let i = 0; i < 10000; i++) {
let car = Math.floor(Math.random() * 3);
let goat = Math.floor(Math.random() * 3);
let choice_of_door = Math.floor(Math.random() * 3);
let switched_door;
for (j = 0; j < 3; j++) {
switched_door = (j != choice_of_door) && (j != goat) ? j : -1;
}
if (car == switched_door) {
switchNwin++;
}
if (car == choice_of_door) {
firstChoiceWin++;
}
}
console.log(switchNwin);
console.log(firstChoiceWin);
My code is producing wrong output. It should be 66% probability of winning. Why's it not coming that?
Did you do some pseudocode first?

yup did it. but this is the part I'm not clear about.
JavaScript:
 for (j = 0; j < 3; j++) {
switched_door = (j != choice_of_door) && (j != goat) ? j : -1;
}

shivajikobardan said:
yup did it. but this is the part I'm not clear about.
JavaScript:
 for (j = 0; j < 3; j++) {
switched_door = (j != choice_of_door) && (j != goat) ? j : -1;
}
I don't know C++, so that means nothing to me.

That said, your program didn't look like I was expecting. It would be interesting to see your pseudocode and see how you interpreted what Monty himself is doing.

JavaScript:
let switchNwin = 0;
let firstChoiceWin = 0;
for (let i = 0; i < 10000; i++) {
let car = Math.floor(Math.random() * 3);
let choice_of_door = Math.floor(Math.random() * 3);
let goat;
// Determine the door revealed with a goat
for (let j = 0; j < 3; j++) {
if (j !== choice_of_door && j !== car) {
goat = j;
break;
}
}
let switched_door;
for (let j = 0; j < 3; j++) {
if (j !== choice_of_door && j !== goat) {
switched_door = j;
break;
}
}
if (car === switched_door) {
switchNwin++;
}
if (car === choice_of_door) {
firstChoiceWin++;
}
}
console.log("Wins after switching doors:", switchNwin);
console.log("Wins after sticking with the original choice:", firstChoiceWin);
Used chatgpt to fix the code. Apparently, you need to conditionally choose the position of goat.

shivajikobardan said:
JavaScript:
let switchNwin = 0;
let firstChoiceWin = 0;
for (let i = 0; i < 10000; i++) {
let car = Math.floor(Math.random() * 3);
let choice_of_door = Math.floor(Math.random() * 3);
let goat;
// Determine the door revealed with a goat
for (let j = 0; j < 3; j++) {
if (j !== choice_of_door && j !== car) {
goat = j;
break;
}
}
let switched_door;
for (let j = 0; j < 3; j++) {
if (j !== choice_of_door && j !== goat) {
switched_door = j;
break;
}
}
if (car === switched_door) {
switchNwin++;
}
if (car === choice_of_door) {
firstChoiceWin++;
}
}
console.log("Wins after switching doors:", switchNwin);
console.log("Wins after sticking with the original choice:", firstChoiceWin);
Used chatgpt to fix the code. Apparently, you need to conditionally choose the position of goat.
I thought there were two goats.

PeroK said:
I thought there were two goats.
what'd be your pseudocode to solve this problem? I'm interested. I'm quite not understanding this chatgpt solution to be honest.

I'm not totally convinced, given that you wrote a program and got the answer ##0.5## and only because you knew that ##0.5## was the wrong answer did you change your code to get the right answer. How do you know your code is simulating the MH game and not some other game with the answer of ##2/3##?

I would have been tempted to simulate the game properly. So, that each time it simulates a real episode of the game. If I were someone who believed the answer should be ##0.5##, I would not be convinced you've proved it is ##2/3##.

shivajikobardan said:
what'd be your pseudocode to solve this problem? I'm interested. I'm quite not understanding this chatgpt solution to be honest.
The way the game works is this:

A car is placed behind a door at random; a goat goes behind each other door.

The contestant picks a door.

If the contestant picks the door with the car, then Monty has to choose which door to open. There's a second random selection there. If the contestant sticks, they win; otherwise, they lose.

If the contestant picks a door with a goat, then Monty opens the other door with a goat. If the contestant sticks, they lose; otherwise, they win.

Then, your output (which you can suppress when you do the full 10,000 simulation) would look something like:

Run 1:

Car is behind door 2.
Contestant picks door 2.
Monty open door 1;
Contestant sticks and wins

Run 2:

Car is behind door 3.
Contestant picks door 1.
Monty opens door 2.
Contestant sticks and loses.

And then you know you are simulating the correct game and not some other game.

scottdave
JavaScript:
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);

Last edited:
shivajikobardan said:
JavaScript:
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);
Line 33: switching doesn't mean you get to pick from the 3 doors anew.

JavaScript:
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 . Thanks @PeroK for the algorithm.

## What is the Monty Hall problem?

The Monty Hall problem is a probability puzzle based on a game show scenario. You are given three doors: behind one door is a car, and behind the other two are goats. You pick a door, and the host, who knows what's behind each door, opens another door, revealing a goat. You are then given the option to stick with your original choice or switch to the remaining unopened door. The puzzle asks whether you should switch or stay to maximize your chances of winning the car.

## How do you simulate the Monty Hall problem in JavaScript?

To simulate the Monty Hall problem in JavaScript, you can write a function that randomly places the car behind one of the doors, simulates the host revealing a goat, and then checks the outcome if the player switches or stays. You would typically run this simulation many times to estimate the probability of winning by switching versus staying. Here's a basic example:

function simulateMontyHall(trials) {    let switchWins = 0;    let stayWins = 0;    for (let i = 0; i < trials; i++) {        const car = Math.floor(Math.random() * 3);        const choice = Math.floor(Math.random() * 3);        const revealed = [0, 1, 2].find(door => door !== car && door !== choice);        const switchChoice = [0, 1, 2].find(door => door !== choice && door !== revealed);        if (choice === car) {            stayWins++;        } else if (switchChoice === car) {            switchWins++;        }    }    return { switchWins, stayWins };}console.log(simulateMontyHall(10000));

## What does the simulation reveal about the best strategy?

The simulation typically reveals that switching doors gives you a higher probability of winning the car. Specifically, the probability of winning if you switch is around 2/3, while the probability of winning if you stay is around 1/3. This outcome aligns with the theoretical solution to the Monty Hall problem.

## Why is switching doors the better strategy?

Switching doors is the better strategy because when you initially pick a door, there is a 1/3 chance that you picked the car and a 2/3 chance that you picked a goat. When the host reveals a goat behind one of the other two doors, if you had initially picked a goat (which happens 2/3 of the time), switching will win you the car. Therefore, switching doors increases your chances of winning from 1/3 to 2/3.