The Nonty Hall Problom Here\'s fun and perhaps surprising statistical riddie. ,
ID: 3586724 • Letter: T
Question
The Nonty Hall Problom Here's fun and perhaps surprising statistical riddie. , In g37eshow, contestants try to guess which of 3 closed doors contain cash prize (goats are bahind tha other two doors). of coursa, the odds of choosing the correct door are 1 in 3 Astwist, the host of the show occasionally opens a door after a contestant akes his or her choice This door is alvays one of the two the contestant did not pick, and is also always one of the goat dcors (note that it 1s always possible to do this, since thaire are two goat doors).At thts point, the contestant has the option of keeping his er her original choice, or sutiching to the other unopened door The question is: is there any benefit to switching doors The aner surprises nany people who hawan't heand the question before. we can answer the probien by running simulations in Java We'1l do it in seversl parts inport javs.util. Randon; iport ava.ut1l.ArrayList; public class Montyli1 I First, write a function called sirulateprizedoer This function will simulate the locstion of the prize in many ganessee the detailed specification below: Function imulate prizecoor Generate a randon array of es, 1s and 2s, representing // hlding pr2ze between door e, door 1, and door 2 nsi:int The nurber of sinulstions to run eturns sins:Arraylist Randon Arraylist of es, is, and 26 tatic Arraylist integer siulate prizodoor int nim) COMPLETE THES PART /J Next, write a function that sirulates the contestant's guesses for nsim sinulations Call this function simulate guess. The specs: // function inulate guess // Return ny strategy for guessing uhich door cOuld ba a randon strategy, one that always guesses 2, whataver pr2ze is behind. Th2.5 his // nsin : int / The nurber of sinulations to generate guesses for Returns // ArrayList of Desses. Each guess is a e, 1, or 2Explanation / Answer
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) {
Random rand = new Random();
ArrayList<Integer> prizeDoor = new ArrayList<>();
for (int i=0; i<nsim; i++){
prizeDoor.add(rand.nextInt(3));
}
return prizeDoor;
}
// 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) {
Random rand = new Random();
ArrayList<Integer> choiceDoor = new ArrayList<>();
for (int i=0; i<nsim; i++){
choiceDoor.add(rand.nextInt(3));
}
return choiceDoor;
}
// 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) {
ArrayList<Integer> goatDoor = new ArrayList<>();
for(int i = 0; i < prizedoors.size(); i++) {
int prize = prizedoors.get(i);
int guess = guesses.get(i);
if (prize == guess && prize != 2)
{
goatDoor.add(2);
}
else
{
goatDoor.add(3- (prize + guess));
}
}
return goatDoor;
}
// 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) {
ArrayList<Integer> switchDoor = new ArrayList<>();
for (int i = 0; i < guesses.size(); i++)
{
switchDoor.add(3 - (guesses.get(i) + goatdoors.get(i)));
}
return switchDoor;
}
// 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) {
int win = 0;
for (int i = 0; i < prizedoors.size(); i++) {
if (prizedoors.get(i) == guesses.get(i)) {
win++;
}
}
return (win*100.0/prizedoors.size());
}
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.