A. Introduction
This is a well-known game, frequently shown on TV.
... 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, which has a goat. He then says to you, “Do you want to pick door No. 2?” Is it to your advantage to switch your choice?
Ms. vos Savant proved that it is to your advantage to switch. But many of her readers, including some mathematics professors, disagreed, arguing that the probability would not change because another door was opened.
B. Your project:
This is E6.22 in our textbook. It sounds like a lot but you might not have realized how much you have learnt. Give a try, don't worry if you could not make it work because it will be discussed in the next class.
C. Hints:
Use Math.random() to get a random number, but you need to research and figure out how to generate a random number between 1-3.
If looping 1000 times is too much to test, first try 10 times or even 5 times.
import java.util.Scanner;
public class MontyHall {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Scanner in2 = new Scanner(System.in);
int input = 0;
int open[] = new int[2];
int car = (int)(3 * Math.random()) + 1;
int switchWin = 0;
int noSwitchWin = 0;
int change = (int) (2 * Math.random());
for (int r = 0; r < 10; r++) {
car = (int)(3 * Math.random()) + 1;
input = 0;
System.out.println("Please select a door: [1] [2] [3]");
input = in.nextInt();
if (input != car) {
System.out.println("We open door #" + (6 - car - input) + " and find that there is a goat!");
open[0] = 6 - car - input;
open[1] = input;
} else {
for (int i = 0; i < 3; i++){
if (i != car){
open[0] = i;
open[1] = input;
}
}
System.out.println("We open door #" + open[0] + "and find that there is a goat!");
}
if (change == 1) {
input = 6 - open[1] - open[0];
if (input == car) {
System.out.println("We open door #" + input + " and reveal that you have won the CAR! Congrats!");
switchWin++;
} else {
System.out.println("We open door #" + input + " and reveal that you have won a GOAT! Better luck next time!");
}
} else {
if (input == car) {
System.out.println("We open door #" + input + " and reveal that you have won the CAR! Congrats!");
noSwitchWin++;
} else {
System.out.println("We open door #" + input + " and reveal that you have won a GOAT! Better luck next time!");
}
}
System.out.println("Simulation Rebooting...");
}
System.out.println("Switching won: " + switchWin + " times and not switching won: " + noSwitchWin + " times");
}
}
class Hall {
public static void main(String[] args) {
int SwitchWin = 0;
int StayWin = 0;
for(int i=1; i<=1000; i++){
int Car = (int) Math.floor(Math.random()*Math.floor(3)) + 1;
int Choice = (int) Math.floor(Math.random()*Math.floor(3)) + 1;
int RevealGoat = 0;
if(Car == Choice){
switch(Car){
case 1: RevealGoat = (int) Math.floor(Math.random()*Math.floor(2)) + 2; break;
case 2: RevealGoat = (int) Math.floor(Math.random()*Math.floor(2)) *2 +1; break;
case 3: RevealGoat = (int) Math.floor(Math.random()*Math.floor(2)) + 1; break;
}
}
else{
RevealGoat = 6 - (Car + Choice);
}
if(Car == Choice){
StayWin++;
}
if(Car == 6 - (RevealGoat + Choice)){
SwitchWin++;
}
}
System.out.println("Switching won " + SwitchWin + " times, and staying won " + StayWin);
}
}