Monty Hall Game Show Problem in Javascript

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.
  • #1
shivajikobardan
674
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
  • #2
Do you understand the game?
 
  • #3
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.
 
  • #4
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.
 
  • #5
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?
 
  • #6
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?
 
  • #7
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;
  }
 
  • #8
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.
 
  • #9
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 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.
 

1. What is the Monty Hall Game Show Problem?

The Monty Hall Game Show Problem is a famous probability puzzle named after the host of the game show "Let's Make a Deal". It involves three doors, one of which hides a prize while the other two hide goats. The player chooses a door, and then the host reveals one of the other doors that does not contain the prize. The player is then given the option to switch their choice to the remaining unopened door. The question is whether it is beneficial for the player to switch their choice or stick with their original door.

2. How does the Monty Hall Game Show Problem relate to Javascript?

The Monty Hall Game Show Problem can be simulated using Javascript code to test different strategies and outcomes. By using variables, conditional statements, and random number generators, Javascript can be used to create a virtual version of the game show and run multiple simulations to analyze the results.

3. What is the optimal strategy for the Monty Hall Game Show Problem?

The optimal strategy for the Monty Hall Game Show Problem is to always switch your choice after the host reveals one of the doors. This strategy gives the player a 2/3 chance of winning the prize, while sticking with the original door only gives a 1/3 chance of winning.

4. Why is the optimal strategy for the Monty Hall Game Show Problem counterintuitive?

The optimal strategy of switching your choice after the host reveals one of the doors can be counterintuitive because it goes against our initial instinct of sticking with our original choice. However, by switching, we are essentially combining the probabilities of the two unchosen doors, increasing our chances of choosing the door with the prize.

5. Can the Monty Hall Game Show Problem be applied to real-life situations?

Yes, the Monty Hall Game Show Problem can be applied to real-life situations where there are multiple choices and a process of elimination. One example is in the game of Deal or No Deal, where contestants are given the option to switch their chosen briefcase after some of the other briefcases are revealed to not contain the grand prize. The same concept of switching to increase the chances of winning applies in this scenario as well.

Similar threads

Replies
12
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
1K
  • General Math
Replies
1
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
910
  • Set Theory, Logic, Probability, Statistics
7
Replies
212
Views
11K
  • Set Theory, Logic, Probability, Statistics
3
Replies
89
Views
7K
  • Set Theory, Logic, Probability, Statistics
Replies
21
Views
13K
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
  • General Math
Replies
30
Views
3K
Back
Top