import java.util.Random; import java.util.ArrayList; public class MontyHall { //
ID: 671830 • Letter: I
Question
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 {
// 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
ArrayList<Integer> randomList = new ArrayList<>();
Random randomGenerator = new Random();
for (int i = 0; i < nsim; i++){
int randomInt = randomGenerator.nextInt(3);
randomList.add(randomInt);
}
return randomList;
}
// 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
ArrayList<Integer> guess = new ArrayList<>();
Random randomGenerator = new Random();
for (int i = 0; i < nsim; i++){
int randomInt = randomGenerator.nextInt(3);
guess.add(randomInt);
}
return guess;
}
// 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
ArrayList<Integer> goat_door = new ArrayList<>();
int j=0;
for (Integer i : prizedoors) {
if(i != guesses.get(j))
goat_door.add(3-i-guesses.get(j));
else if(i == 0)
goat_door.add(1);
else
goat_door.add(3-i);
j++;
}
return goat_door;
}
// 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
ArrayList<Integer> switch_guess = new ArrayList<>();
int j=0;
for (Integer i : goatdoors) {
if(i != guesses.get(j))
switch_guess.add(3-i-guesses.get(j));
else if(i == 0)
switch_guess.add(1);
else
switch_guess.add(3-i);
j++;
}
return switch_guess;
}
// 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
int win = 0;
int j=0;
for (Integer i : prizedoors) {
if (i==guesses.get(j))
win++;
j++;
}
return ((win*1.0)/j)*100.0;
}
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.