Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Monty Hall Problem Simulation

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Monty Hall Problem Simulation

    I'm trying to write a main method that proves the Monty Hall scenario with a for loop. The Monty Hall problem is based off of a probability riddle. You are playing a game where there are 3 closed doors, behind one is a car but behind the other two are goats. After you pick a door, the game host shows you one of the other doors that has a goat in it. He then asks if you want to change your guess. So say you first guess door 1, the host then opens door 3 to show you a goat so he asks if you want to switch your answer to door 2. Probability tells us that if you switch there is a 2/3 chance that you will win the car (not 1/2 like most people think at first). The object of my method is to prove this by using a for loop and running it a large number of times ( i used 1000000) to prove the percent of winning when switching is 66.7%. The problem is when I run my code I keep getting 50% as my output and I cannot see why.

    import java.util.Random;
     
    public class MontySim {
    	public static void main(String[] args) {
    		Random ran = new Random();
    		int n = 1000000;
    		int wins = 0;
     
    		for (int i = 0; i < n; i++) {
    			int winningNumber = ran.nextInt(3) + 1;
    			int losingNumber = ran.nextInt(3) + 1;
     
    			while (losingNumber == winningNumber) {
    				losingNumber = ran.nextInt(3) + 1;
    				}
     
    			int firstSelection = ran.nextInt(3) + 1;
    			while ((firstSelection == losingNumber)) {
    				firstSelection = ran.nextInt(3) + 1;
    			}
     
    			int secondSelection = ran.nextInt(3) + 1;
     
    			while ((secondSelection == firstSelection)
    					|| (secondSelection == losingNumber)) {
    				secondSelection = ran.nextInt(3) + 1;
    			}
     
    			if (secondSelection == winningNumber) {
    				wins ++;
    			}
    		}
    		double percent = 100 * wins / n;
     
    		System.out.print(percent + "%");
    	}
     
    }

    Any thoughts on why I'm not getting 66.7%? I thought i set it up well. I have the random generator making 4 numbers, the door where the car is (winningNumber), the door the host shows you (losingNumber), the door your choose first (firstSelection) and the door you switch to (secondSelection). I also made while statements to make sure the that the value of losingNumber cannot be the same as winningNumber, as well as making sure the firstSelection cannot be the same as the losingNumber, as well as making sure the secondSelection cannot be the same as firstSelection or the losingNumber.

    Thanks

    Casey
    Last edited by caseyd; March 5th, 2011 at 12:42 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Monty Hall Problem Simulation

    The problem is in the order you've done things (thus altering the statistics). The player can pick any door (with equal likelyhood of picking any of the 3), then the host must decide of the remaining two which one to open.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    caseyd (March 5th, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Monty Hall Problem Simulation

    that fixed it! I was defining the door the host opened before the the first choice! thank you, its working now

    CD

Similar Threads

  1. Keypress simulation problems
    By ummix in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2010, 07:31 AM