Question

please help in java Monty Hall was a television game show host years ago. The contestant...

please help in java

Monty Hall was a television game show host years ago. The contestant would have 3 doors to choose from. There was always a prize behind one door and goats behind the other two doors. The contestant would choose a door.

Monty Hall would then open a door following these rules: Not a door the contestant chose and not the winning door. Monty ALWAYS showed a goat. He'd then ask the contestant if they wanted to change doors. After the contestant made their final choice, Monty Hall would open the chosen door.

Program Requirements:

Your job here is to simulate this. Make it automatic. Randomly choose the winning door. Then randomly choose one of the 3 doors as the contestant's choice. Run this at least 100,000 times to calculate an accurate probability. You will be calculating for the probability of winning if the contestant switches doors when given the opportunity. Also compute the probability of winning if they don't switch.

There has a been a lot of discussion over the years about whether or not the contestant should switch doors. Here is a clip from the movie 21 (Links to an external site.)Links to an external site. that discusses this choice.

Some examples of the potential output from past students (with the answers hidden to protect the innocent).  

Example 1:

There is a xxx.xx% chance that you will win if you switch doors.
There is a yyyy.yy% chance that you will win if you do NOT switch doors.

Example 2:

Without switching:
xxxx / 100000 wins
xx.xxxx% chance to win

With switching:
yyyy / 100000 wins
yy.yyyy% chance to win

Example 3:

The probability of the contestant winning if they don't switch is : 0.xxxx
The probability of the contestant winning if they do switch is : 0.yyyy

again(y/n): y

The probability of the contestant winning if they don't switch is : 0.xxxx <different but similar values than the first run>
The probability of the contestant winning if they do switch is : 0.yyyy

Since we are calculating WINS, the two percentages should add up to 100% or 1

Additional Requirements:

  • Do not hard code the answer. It must be calculated and averaged over a number of runs. If <when> I change the number of iterations of the loop, the average should change <minimally>.
  • You can start testing with a small number of iterations but the program should ultimately loop 100,00 times. DON'T put any print statements inside the loop or you may kill my laptop...
0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for the question, here is the MontyHall class that determines the probabilities of staying with the same door or switching the door

All important lines of code are commented so that you can understand whats going on

========================================================================================

import java.util.Random;
public class MontyHall{

    private static Random gen = new Random();

    public static void main(String[] args){
        int switchWins = 0;
        int stayWins = 0;

        for(int plays = 1;plays <= 100000;plays++ ){
            int[] doors = {0,0,0};//0 is a goat, 1 is a car
            doors[gen.nextInt(3)] = 1;//put a car in a random door
            int choice = gen.nextInt(3); //guess a door, any door
            int shown; //the shown door
            do{
                shown = gen.nextInt(3);
                //don't show the winner or the choice
            }while(doors[shown] == 1 || shown == choice);
            stayWins += doors[choice];//if you won by staying, count it
            //the switched (last remaining) door is (3 - choice - shown), because 0+1+2=3
            switchWins += doors[3 - choice - shown];
        }
        System.out.println("Without switching: ");
        System.out.println(stayWins+"/1000000 wins");
        System.out.println(String.format("%.4f",stayWins/10000.00)+"% chance to win");
        System.out.println("With switching: ");
        System.out.println(switchWins+"/1000000 wins");
        System.out.println(String.format("%.4f",switchWins/10000.00)+"% chance to win");


    }
}

=================================================================================

thanks !

let me know incase you have any questions or problem.

Add a comment
Answer #2

import java.io.*;
import java.util.Random;
class Monty
{
   public static void main(String args[])
   {
       int original,my_choice,first_show,user_change;
       long win = 0, change = 0, cases=100000;
       Random rand = new Random();
       for(long i=0;i<cases;i++)
       {
           original = rand.nextInt(3);   // Actual door
           my_choice = rand.nextInt(3);   // Contestent choose
           user_change = rand.nextInt(2);   // If user wants to change answer
           if(user_change == 1)
           {
               if(my_choice != original)   // User changed the answer and the answer is correct
               {
                   win++;
                   change++;
               }
           }
           else if(my_choice == original)       // user didn't changed the answer but the answer is correct
               win++;
       }
       System.out.println("\nWith switching the chances of wining "+((double)change)/((double)cases));
       System.out.println("\nWithout switching the chances of wining "+((double)(win-change))/((double)cases));
       System.out.println("\nChances of wining "+((double)win)/((double)cases));
   }
}

Set the file name as Monty.java

In Linux use javac Monty.java and java Monty.

Please let me know if anything else modification is required. If you like the answer please hit the thumbs up. Best of luck !!!

Add a comment
Know the answer?
Add Answer to:
please help in java Monty Hall was a television game show host years ago. The contestant...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the Monty Hall problem. Let’s label the door with the car behind it a and...

    Consider the Monty Hall problem. Let’s label the door with the car behind it a and the other two doors b and c. In the game the contestant chooses a door and then Monty chooses a door, so we can label each outcome as ‘contestant followed by Monty’, for example ab means the contestant chose a and Monty chose b. (a) Make a 3 × 3 probability table showing probabilities for all possible outcomes. (3 marks) (b) Make a probability...

  • Question 1: Consider the following Monty Hall problem. Suppose you are on a game show, and...

    Question 1: Consider the following Monty Hall problem. Suppose you are on a game show, and you are given the choice of three doors. Behind one door is a car, behind the others, goats. You pick a door, say #1, and the host, who knows what is behind the doors, opens another door, say #3, which has a goat. Here we assume that the host cannot open the door to expose the car and when he can open either of...

  • 5. Consider the Monty Hall Problem. A game show host shows you three doors, and indicates...

    5. Consider the Monty Hall Problem. A game show host shows you three doors, and indicates that one of them has a car behind it, while the other two have goats. You win a car if you end up choosing a door with a car behind it. The game is conducted as follows: • You pick an initial door out of the three available. • The game show hosts then opens a door (out of the remaining two doors) with...

  • e. Application of Bayes: Using Bayes theorem driven in item d, show that in the following problem switching is the bett...

    e. Application of Bayes: Using Bayes theorem driven in item d, show that in the following problem switching is the better strategy for the player and makes the probability of wining as 2/3, while not switching gives the probability of wining 1/3. Monty Hall: There are 3 doors 1, 2, 3. Randomly, and equally likely, behind one of the door there is a prize, and other two are empty. The player choose one door. He does not open the door...

  • 1.3 Cars and goats: the Monty Hall dilemma On Sunday September 9, 1990, the following question...

    1.3 Cars and goats: the Monty Hall dilemma On Sunday September 9, 1990, the following question appeared in the "Ask Marilyn" column in Parade, a Sunday supplement to many newspapers across the United States: Suppose you're on a game show, and you're given the choice of three doors; behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3,...

  • 1. Game simulation, Java programming. In the game show Let’s Make a Deal, a contestant is...

    1. Game simulation, Java programming. In the game show Let’s Make a Deal, a contestant is presented with three doors. Behind one of them is a valuable prize. After the contestant chooses a door, the host opens one of the other two doors (never revealing the prize, of course). The contestant is then given the opportunity to switch to the other unopened door. Should the contestant do so? Intuitively, it might seem that the contestant’s initial choice door and the...

  • I need help fixing my python3 code. I am trying to get my Monty hall game...

    I need help fixing my python3 code. I am trying to get my Monty hall game working correctly. Below is my code. When I run the code it repeats the first question and doesn't work until the 3rd attempt or sometimes more than that. How do I fix this? I also need to be able to make it run 5 times in a row.(like ask the user at least 5 times, so it is re-playable. I added a image of...

  • Monty Hall Problem - Suppose you are going to be on a game show, and you...

    Monty Hall Problem - Suppose you are going to be on a game show, and you will be given the choice of three doors: Behind one door is a car; behind the other two doors are goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your...

  • The Game: Suppose you're on a game show, and you're given the choice of 3 doors....

    The Game: Suppose you're on a game show, and you're given the choice of 3 doors. Behind one door is a car, behind the others, goats. You start by choosing a door, say number 1, which remains closed for now. The game show host, who knows what's behind the doors, opens another door, say number 3, which reveals a goat. He says to you, "You've already chosen door number 1, now that I've shown you a goat behind door number...

  • In the three-door Monty Hall problem, there are two stages to the decision, the initial pick...

    In the three-door Monty Hall problem, there are two stages to the decision, the initial pick followed by the decision to stick with it or switch to the only other remaining alternative after the host has shown an incorrect door. An extension of the basic problem to multiple stages goes as follow. Suppose there are four doors, one of which is a winner. The host says: You point to one of the doors, and then I will open one of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT