Hey guys. In my Data Structures Class today somehow we ended up talking about the Monty Hall Problem for a little while. Anyways I got bored and wrote code to statistically check if it is correct (even thought I know it is).

For those of you who don't know what the problem is it goes something like this:

You are on a game show and have to pick one of three doors. Behind one door is a brand new car and the other two have nothing behind them. You make your selection. After you make your selection the game show host opens one of the doors that you did not select and reveals that there is nothing behind it. Now the host gives you the option to switch your answer to the other remaining door or keep the door you selected in the first place. Should you switch is does it not matter? Check the code to see the answer for yourself! (hint... ten million trials usually gets the exact percentages although it takes a few seconds to run)

Here is the code... please check to make sure I did it correctly. Also I know it may not be the most efficient way to write the code but I was in a rush and that's the best way I could think to write it at the time.

```
import java.util.*;
public class MontyHall
{
public static void main(String[] args)
{
Scanner key = new Scanner(System.in);
Random gen = new Random();
System.out.print("Enter number of trials to run: ");
int NUMOFTRIALS = key.nextInt();
System.out.print("Stay Always (1) or Switch Always (2): ");
int choice = key.nextInt();
int numCorrect = 0;
int pick,ans,show;
ArrayList<Integer> doors = new ArrayList<Integer>();
if(choice == 1) // always stay
{
for(int i = 0; i < NUMOFTRIALS; i++)
{
doors.clear();
doors.add(1);
doors.add(2);
doors.add(3);
pick = gen.nextInt(3);
ans = gen.nextInt(3);
doors.remove(ans);
if(!doors.contains(pick)) show = doors.get(0);
else
{
if(doors.get(0) == pick) show = doors.get(1);
else show = doors.get(0);
}
if(show == pick) System.out.println("Something is wrong!!!");
if(pick == ans) numCorrect++;
}
}
else if(choice == 2) // always switch
{
int other = 22;
for(int i = 0; i < NUMOFTRIALS; i++)
{
doors.clear();
doors.add(0);
doors.add(1);
doors.add(2);
pick = gen.nextInt(3);
ans = gen.nextInt(3);
doors.remove(ans);
if(!doors.contains(pick))
{
int ran = gen.nextInt(2);
show = doors.get(ran);
if(ran == 1)
other = doors.get(0);
if(ran == 0)
other = doors.get(1);
}
else
{
if(doors.get(0) == pick)
{
show = doors.get(1);
other = ans;
}
else
{
show = doors.get(0);
other = ans;
}
}
if(show == pick || other == 22) System.out.println("Something is wrong!!!");
pick = other;
if(pick == ans)numCorrect++;
}
}
double percCorr = (double)numCorrect/(double)NUMOFTRIALS;
System.out.printf("\n%.2f%s correct answers.", percCorr*100, "%");
}
}
```