| | |
Monty Hall Problem
![]() |
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.
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.
Java Syntax (Toggle Plain Text)
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, "%"); } }
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
You're getting the right answers and the logic seems OK. If you're looking for a critique of the code, I'd use more descriptive variable names and add comments so people can follow the logic more easily. In particular, you have a variable that you call other and initialize to 22, which is a value that will be illegal. I'd have it be -999999 or something that stands out more and stick some comment in explaining what -999999 stands for. I was puzzling over what a value of 22 might represent for a while before I realized it was a bogus flag value, set intentionally to be a bogus flag value. -999999 stands out right away as a flag value.
•
•
Join Date: Sep 2008
Posts: 1,566
Reputation:
Solved Threads: 196
Von Savant problem? I might have spelled her name wrong. I don't remember the reasoning and this problem is actually tricky, but I think I remember the answer: yes you should switch. My guess at the reasoning would be that after the host showing the one door, you now have a 50% chance of choosing the right one, which is up from your previous 33% chance. [update: this is wrong, if you read the wiki on the subject the picture clearly depicts why] Either way, this problem caused quite an uproar in the mathematical community with mathematicians who said she was wrong, and then it turned out that they were wrong.
Last edited by BestJewSinceJC; Jun 19th, 2009 at 1:16 am.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
Von Savant problem? I might have spelled her name wrong. I don't remember the reasoning and this problem is actually tricky, but I think I remember the answer: yes you should switch. My guess at the reasoning would be that after the host showing the one door, you now have a 50% chance of choosing the right one, which is up from your previous 33% chance. [update: this is wrong, if you read the wiki on the subject the picture clearly depicts why] Either way, this problem caused quite an uproar in the mathematical community with mathematicians who said she was wrong, and then it turned out that they were wrong.
•
•
•
•
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 #1, and the host, who knows what's behind the doors, opens another door, say #3, which has a goat. He says to you, "Do you want to pick door #2?" Is it to your advantage to switch your choice of doors?
Craig F. Whitaker
Columbia, Maryland
Here is Marilyn's later explanation.
•
•
•
•
Good heavens! With so much learned opposition, I'll bet this one is going to keep math classes all over the country busy on Monday.
My original answer is correct. But first, let me explain why your answer is wrong. The winning odds of 1/3 on the first choice can't go up to 1/2 just because the host opens a losing door. To illustrate this, let's say we play a shell game. You look away, and I put a pea under one of three shells. Then I ask you to put your finger on a shell. The odds that your choice contains a pea are 1/3, agreed? Then I simply lift up an empty shell from the remaining other two. As I can (and will) do this regardless of what you've chosen, we've learned nothing to allow us to revise the odds on the shell under your finger.
So in my view, the mathematicians were right, she was wrong.
http://www.marilynvossavant.com/articles/gameshow.html
My take on the problem was that the host MUST show you an empty door after you make your first choice EVERY time reguardless of if you picked correctly the first time. This assumes that the host is not trying to make you lose.
If you have these assumptions then you can difinitivly answer saying that you should switch everytime because:
- the first time you select a door you have a 1/3 chance of being correct.
- this means there is a 1/3 chance of the other two doors being empty.
- and by contrast a 2/3 chance of ONE of the other doors containing the car.
- therefore if the host shows you one of the empty doors there is a 2/3 chance that the other door has the car behind it.
My program demonstrates this with about 66% winning percentage when selecting "Switch Always" and about a 33% winning percentage when selecting "Stay always"
If you have these assumptions then you can difinitivly answer saying that you should switch everytime because:
- the first time you select a door you have a 1/3 chance of being correct.
- this means there is a 1/3 chance of the other two doors being empty.
- and by contrast a 2/3 chance of ONE of the other doors containing the car.
- therefore if the host shows you one of the empty doors there is a 2/3 chance that the other door has the car behind it.
My program demonstrates this with about 66% winning percentage when selecting "Switch Always" and about a 33% winning percentage when selecting "Stay always"
I saw this problem in a movie (I think its name was 21) and I never thought about it. After I saw it here this morning I started to think about it. I had worked on it for 12 hours and I finally figured it out half an hour ago in the shower. thank you for posting it here. I think I will try to write the program myself. If I write it I'll post the link here. (man figuring that out made me really happy
)
) ![]() |
Similar Threads
- Problem with Windows Update and WinXP (Web Browsers)
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- Installing Windows 98 On VMware. Floppy problem (Windows 95 / 98 / Me)
- Rtvscan.exe has generated errors (Windows NT / 2000 / XP)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP)
- Redhat Linux 6.2 - ipop3d problem? (*nix Software)
- Problem with T720 (Cellphones, PDAs and Handheld Devices)
- Connection Problems (Networking Hardware Configuration)
- OOL is amazin' (Networking Hardware Configuration)
Other Threads in the Java Forum
- Previous Thread: 'package does not exist' error
- Next Thread: Hmm, UI manager and static JProgressBar?
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






