Monty Hall Problem

Reply

Join Date: Feb 2009
Posts: 36
Reputation: smoore is an unknown quantity at this point 
Solved Threads: 1
smoore's Avatar
smoore smoore is offline Offline
Light Poster

Monty Hall Problem

 
0
  #1
Jun 17th, 2009
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.
  1. import java.util.*;
  2.  
  3. public class MontyHall
  4. {
  5. public static void main(String[] args)
  6. {
  7. Scanner key = new Scanner(System.in);
  8. Random gen = new Random();
  9.  
  10. System.out.print("Enter number of trials to run: ");
  11. int NUMOFTRIALS = key.nextInt();
  12. System.out.print("Stay Always (1) or Switch Always (2): ");
  13. int choice = key.nextInt();
  14. int numCorrect = 0;
  15. int pick,ans,show;
  16. ArrayList<Integer> doors = new ArrayList<Integer>();
  17.  
  18. if(choice == 1) // always stay
  19. {
  20. for(int i = 0; i < NUMOFTRIALS; i++)
  21. {
  22. doors.clear();
  23. doors.add(1);
  24. doors.add(2);
  25. doors.add(3);
  26.  
  27. pick = gen.nextInt(3);
  28. ans = gen.nextInt(3);
  29.  
  30. doors.remove(ans);
  31.  
  32. if(!doors.contains(pick)) show = doors.get(0);
  33. else
  34. {
  35. if(doors.get(0) == pick) show = doors.get(1);
  36. else show = doors.get(0);
  37. }
  38.  
  39. if(show == pick) System.out.println("Something is wrong!!!");
  40. if(pick == ans) numCorrect++;
  41. }
  42. }
  43. else if(choice == 2) // always switch
  44. {
  45. int other = 22;
  46. for(int i = 0; i < NUMOFTRIALS; i++)
  47. {
  48. doors.clear();
  49. doors.add(0);
  50. doors.add(1);
  51. doors.add(2);
  52.  
  53. pick = gen.nextInt(3);
  54. ans = gen.nextInt(3);
  55.  
  56. doors.remove(ans);
  57.  
  58. if(!doors.contains(pick))
  59. {
  60. int ran = gen.nextInt(2);
  61. show = doors.get(ran);
  62.  
  63. if(ran == 1)
  64. other = doors.get(0);
  65. if(ran == 0)
  66. other = doors.get(1);
  67. }
  68. else
  69. {
  70. if(doors.get(0) == pick)
  71. {
  72. show = doors.get(1);
  73. other = ans;
  74.  
  75. }
  76. else
  77. {
  78. show = doors.get(0);
  79. other = ans;
  80.  
  81. }
  82.  
  83. }
  84.  
  85. if(show == pick || other == 22) System.out.println("Something is wrong!!!");
  86.  
  87. pick = other;
  88.  
  89. if(pick == ans)numCorrect++;
  90. }
  91. }
  92. double percCorr = (double)numCorrect/(double)NUMOFTRIALS;
  93. System.out.printf("\n%.2f%s correct answers.", percCorr*100, "%");
  94. }
  95. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Monty Hall Problem

 
0
  #2
Jun 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Monty Hall Problem

 
0
  #3
Jun 18th, 2009
Like Vernon says, but also you can make a "bogus" value totally clear at zero overhead by declaring it as static final:
public static final int BOGUS = -999999;
then:
var = BOGUS;
if (var == BOGUS) { ...
etc
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,566
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Monty Hall Problem

 
0
  #4
Jun 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Monty Hall Problem

 
0
  #5
Jun 19th, 2009
Originally Posted by BestJewSinceJC View Post
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.
The mathematicians weren't wrong, in my view. The way the original problem is phrased leaves out a key assumption (that the host MUST show a bad door and allow the contestant to switch). If that's the RULE, then yes, she's right. Here's the original question:

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
Nothing in the original question states that the host is REQUIRED to show you a losing door and allow you to switch. She's reading into it that the host IS required to, but that was never stated.


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.
The red part is the invalid assumption. If the host is trying to make you lose, he/she might only offer you the option of switching if you pick the right door the first time. Nothing in the original game rules stipulate that the host always has to let you switch, so her analysis is invalid. If the host DOES have to show you a losing door and let you switch, then she would be right.

So in my view, the mathematicians were right, she was wrong.

http://www.marilynvossavant.com/articles/gameshow.html
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 36
Reputation: smoore is an unknown quantity at this point 
Solved Threads: 1
smoore's Avatar
smoore smoore is offline Offline
Light Poster

Re: Monty Hall Problem

 
0
  #6
Jun 19th, 2009
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"
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 48
Reputation: yilmazhuseyin is an unknown quantity at this point 
Solved Threads: 5
yilmazhuseyin's Avatar
yilmazhuseyin yilmazhuseyin is offline Offline
Light Poster

Re: Monty Hall Problem

 
1
  #7
Jun 19th, 2009
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 )
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC