* The Monty Hall Problem * Here\'s a fun and perhaps surprising statistical ridd
ID: 673196 • Letter: #
Question
* The Monty Hall Problem * Here's a fun and perhaps surprising statistical riddle. * In a gameshow, contestants try to guess which of 3 closed doors contain a cash prize * (goats are behind the other two doors). Of course, the odds of choosing the correct door are 1 in 3. * As a twist, the host of the show occasionally opens a door after a contestant makes his or her choice. * This door is always one of the two the contestant did not pick, and is also always one of the goat doors * (note that it is always possible to do this, since there are two goat doors). At this point, the contestant * has the option of keeping his or her original choice, or swtiching to the other unopened door. * The question is: is there any benefit to switching doors? * The answer surprises many people who haven't heard the question before. * We can answer the problem by running simulations in Java. * We'll do it in several parts. */ Complete all of the functons in the java program import java.util.Random; import java.util.ArrayList; public class MontyHall { // First, write a function called simulate_prizedoor. // This function will simulate the location of the prize in many games -- see the detailed specification below: // // Function // -------- // simulate_prizedoor // // Generate a random array of 0s, 1s, and 2s, representing // hiding a prize between door 0, door 1, and door 2 // // Parameters // ---------- // nsim : int // The number of simulations to run // // Returns // ------- // sims : ArrayList // Random ArrayList of 0s, 1s, and 2s // static ArrayList<Integer> simulate_prizedoor(int nsim) { // COMPLETE THIS PART } // Next, write a function that simulates the contestant's guesses for nsim simulations. // Call this function simulate_guess. The specs: // // Function // -------- // simulate_guess // // Return any strategy for guessing which door a prize is behind. This // could be a random strategy, one that always guesses 2, whatever. // // Parameters // ---------- // nsim : int // The number of simulations to generate guesses for // // Returns // ------- // guesses : ArrayList // An ArrayList of guesses. Each guess is a 0, 1, or 2 // static ArrayList<Integer> simulate_guess(int nsim) { // COMPLETE THIS PART } // Next, write a function, goat_door, to simulate randomly revealing one of the goat doors // that a contestant didn't pick. // // Function // -------- // goat_door // // Simulate the opening of a "goat door" that doesn't contain the prize, // and is different from the contestants guess // // Parameters // ---------- // prizedoors : ArrayList // The door that the prize is behind in each simulation // guesses : ArrayList // THe door that the contestant guessed in each simulation // // Returns // ------- // goats : ArrayList // The goat door that is opened for each simulation. Each item is 0, 1, or 2, and is different // from both prizedoors and guesses // static ArrayList<Integer> goat_door(ArrayList<Integer> prizedoors, ArrayList<Integer> guesses) { // COMPLETE THIS PART } // Write a function, switch_guess, that represents the strategy of always switching a guess after the goat door is opened. // // Function // -------- // switch_guess // // The strategy that always switches a guess after the goat door is opened // // Parameters // ---------- // guesses : ArrayList // Array of original guesses, for each simulation // goatdoors : ArrayList // Array of revealed goat doors for each simulation // // Returns // ------- // The new door after switching. Should be different from both guesses and goatdoors // static ArrayList<Integer> switch_guess(ArrayList<Integer> guesses, ArrayList<Integer> goatdoors) { // COMPLETE THIS PART } // Last function: write a win_percentage function that takes an array of guesses and prizedoors, // and returns the percent of correct guesses // // Function // -------- // win_percentage // // Calculate the percent of times that a simulation of guesses is correct // // Parameters // ----------- // guesses : ArrayList // Guesses for each simulation // prizedoors : ArrayList // Location of prize for each simulation // // Returns // -------- // percentage : number between 0 and 100 // The win percentage // static double win_percentage(ArrayList<Integer> guesses, ArrayList<Integer> prizedoors) { // COMPLETE THIS PART } public static void main(String[] Args) { int number_of_trials = 10; // DO NOT MAKE CHANGES TO THE MAIN FILE ArrayList<Integer> prize = simulate_prizedoor(number_of_trials); System.out.println(prize); ArrayList<Integer> guess = simulate_guess(number_of_trials); System.out.println(guess); ArrayList<Integer> goat = goat_door(prize, guess); System.out.println(goat); ArrayList<Integer> new_door = switch_guess(guess, goat); System.out.println(new_door); System.out.println(" Win Percentage when not changing the door: " + win_percentage(guess, prize)); System.out.println(" Win Percentage after changing the door: " + win_percentage(new_door, prize)); // DO NOT MAKE CHANGES TO THE MAIN FILE } }
Explanation / Answer
import java.util.Random;
import java.util.ArrayList;
public class MontyHall {
static ArrayList<Integer> simulate_prizedoor(int nsim) {
//randon number generation
Random rn = new Random();
//arrylist declared
ArrayList<Integer> doors = new ArrayList<Integer>();
//storing random values
for(int i=0;i<nsim;i++){
doors.add(rn.nextInt(2));
}
return doors; //reutrning arrylist of random values
}
static ArrayList<Integer> simulate_guess(int nsim) {
//arrylist declared
ArrayList<Integer> doors = new ArrayList<Integer>();
//storing user choice values
for(int i=0;i<nsim;i++){
System.out.println("Enter your choice 0 1 2: ");
int num = sc.nextInt();
doors.add(num);
}
return doors; //reutrning arrylist of values read from user
}
static ArrayList<Integer> goat_door(ArrayList<Integer> prizedoors, ArrayList<Integer> guesses) {
//arrylist declared
ArrayList<Integer> doors = new ArrayList<Integer>();
for(int i=0;i<prizedoors.size();i++){
if(prizedoors.get(i) == guesses.get(i)){
doors.add(2);
}
else{
doors.add(3);
}
}
return doors;
}
//returns percentage of winning
static double win_percentage(ArrayList<Integer> guesses, ArrayList<Integer> prizedoors) {
double correct_Guess=0;
for(int i=0;i<prizedoors.size();i++){
if(prizedoors.get(i) == guesses.get(i)){
correct_Guess = correct_Guess+1;
}
}
return (correct_Guess/guesses.size())*100;
}
//main
public static void main(String[] Args) {
int number_of_trials = 10;
// DO NOT MAKE CHANGES TO THE MAIN FILE
ArrayList<Integer> prize = simulate_prizedoor(number_of_trials);
System.out.println(prize);
ArrayList<Integer> guess = simulate_guess(number_of_trials);
System.out.println(guess);
ArrayList<Integer> goat = goat_door(prize, guess);
System.out.println(goat);
ArrayList<Integer> new_door = switch_guess(guess, goat);
System.out.println(new_door);
System.out.println(" Win Percentage when not changing the door: " + win_percentage(guess, prize));
System.out.println(" Win Percentage after changing the door: " + win_percentage(new_door, prize));
// DO NOT MAKE CHANGES TO THE MAIN FILE
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.