Monty Hall Game Show Problem in Javascript

  • Context: Comp Sci 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Monty hall Probability
Click For Summary
SUMMARY

The Monty Hall Game Show Problem involves a contestant choosing one of three doors, behind one of which is a car and behind the others are goats. The host reveals a goat behind one of the remaining doors and offers the contestant a choice to stick with their original selection or switch to the other unopened door. A simulation program in JavaScript is designed to run 10,000 iterations to estimate the winning probabilities for both strategies. The correct implementation shows that switching doors yields a winning probability of approximately 66%, while sticking with the original choice results in about 33%.

PREREQUISITES
  • Understanding of probability theory and simulations
  • Proficiency in JavaScript, particularly with loops and random number generation
  • Familiarity with conditional logic and control structures in programming
  • Basic knowledge of the Monty Hall problem and its implications
NEXT STEPS
  • Implement the Monty Hall simulation in Python to compare results with JavaScript
  • Explore the mathematical proof behind the 2/3 probability of winning by switching
  • Learn about Monte Carlo simulations and their applications in probability problems
  • Investigate variations of the Monty Hall problem, such as increasing the number of doors
USEFUL FOR

Mathematicians, computer science students, game theorists, and anyone interested in probability and simulation programming.

shivajikobardan
Messages
637
Reaction score
54
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?
 
Physics news on Phys.org
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.
 
  • #10
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.
 
  • #11
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.
 
  • #12
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##.
 
  • #13
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.
 
  • Like
Likes   Reactions: scottdave
  • #14
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:
  • #15
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.
 
  • #16
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.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 53 ·
2
Replies
53
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 212 ·
8
Replies
212
Views
16K
  • · Replies 89 ·
3
Replies
89
Views
10K
  • · Replies 21 ·
Replies
21
Views
15K
  • · Replies 11 ·
Replies
11
Views
2K