943,648 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1837
  • Java RSS
Jun 17th, 2009
0

Monty Hall Problem

Expand Post »
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.
Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 39
Solved Threads: 1
Light Poster
smoore is offline Offline
36 posts
since Feb 2009
Jun 18th, 2009
0

Re: Monty Hall Problem

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Jun 18th, 2009
0

Re: Monty Hall Problem

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
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is online now Online
5,764 posts
since Apr 2008
Jun 19th, 2009
0

Re: Monty Hall Problem

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jun 19th, 2009
0

Re: Monty Hall Problem

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:

Quote ...
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.

Quote ...
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
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Jun 19th, 2009
0

Re: Monty Hall Problem

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"
Reputation Points: 39
Solved Threads: 1
Light Poster
smoore is offline Offline
36 posts
since Feb 2009
Jun 19th, 2009
1

Re: Monty Hall Problem

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 )
Reputation Points: 31
Solved Threads: 5
Light Poster
yilmazhuseyin is offline Offline
48 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Connect to https
Next Thread in Java Forum Timeline: Hmm, UI manager and static JProgressBar?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC