Log in

View Full Version : Monty Hall [Java]


Jufjufjuf
January 2nd, 2012, 11:24 AM
Hey guys, it's 10AM and I didn't get any sleep so excuse me if I'm not very clear, or if my coding is highly inefficient. :yawn: Anyway, my friend and I were talking and he kept complaining that the Monty Hall problem just isn't correct in it's findings, even when played a bunch of times. I showed him the logic w/e and he wouldn't believe me. So I ended up making a program to simulate the Monty Hall problem a million times. Now he says my coding is flawed... I ask you to show me where my coding is flawed (in relation to the problem not efficiency and stuff) or if it's correct. I'll show him this thread when he wakes up lol

Syntax Highlighted;
http://pastebin.com/UZG3bbsA

Raw Source;
import java.util.Random;

public class main {

public static void main(String[] args) {

//Initialise variables
long car;
long guess;
long hostopened;
Random randomGenerator = new Random();
long win_keep = 0;
long win_switch = 0;
long loose_keep = 0;
long loose_switch = 0;

for(long count = 0; count != 1000000; count++){ //run the simulation 1000000 times

//Assign the car door, your guess and the door the host opens to a random number of (0,1 or 2)
car = randomGenerator.nextInt(3);
guess = randomGenerator.nextInt(3);
hostopened = randomGenerator.nextInt(3);

//According to the problem, the host will only open the door that doesn't have the car.
while(hostopened==car||hostopened==guess){ //So we enter this loop until the door opened by the host isn't the car. IDGAF that it's inefficient.
hostopened = randomGenerator.nextInt(3);
}
if(guess == car){ //If you did choose the door initially then you win if you kept, and lost if you switched.
loose_switch++;
win_keep++;
}else{ //If you didn't guess the car initially then you win if you switch, loose if you keep.
win_switch++;
loose_keep++;
}
}
//Self Explanatory
System.out.println("While changing your door you won " + win_switch + " and lost " + loose_switch + " times.");
System.out.println("While keeping your door you won " + win_keep + " and lost " + loose_keep + " times");

}
}

TheMatrix
January 2nd, 2012, 04:28 PM
This computer does not have Java, so I'll make a Perl version of it:
#!/usr/bin/perl
#===============================================================================
#
# FILE: montyhall.pl
#
# USAGE: ./montyhall.pl
#
# DESCRIPTION: Perl version of the Monty Hall paradox
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Thomas(TheMatrix)
# VERSION: 1.0
# CREATED: 02/01/12 13:07:37
# REVISION: ---
#===============================================================================

use strict;
use warnings;

#Initialize variables
my( $car, $guess, $hostopened, $win_keep, $win_switch, $loose_keep, $loose_switch );

foreach( 1..1000000 ) {
#Assign the car door, your guess and the door the host opens to a random number of (0,1 or 2)
$car = int( rand( 3 ) );
$guess = int( rand( 3 ) );
$hostopened = int( rand( 3 ) );

#According to the problem, the host will only open the door that doesn't have the car.
while( ($hostopened == $car) || ($hostopened == $guess) ) {
#So we enter this loop until the door opened by the host isn't the car. IDGAF that it's inefficient.
$hostopened = int( rand( 3 ) );
}

if( $guess == $car ) {
#If you did choose the door initially then you win if you kept, and lost if you switched.
$loose_switch++;
$win_keep++;
} else {
#If you didn't guess the car initially then you win if you switch, loose if you keep.
$win_keep++;
$loose_keep++;
}
}

print "While changing your door you won ". $win_switch ." and lost". $loose_switch ." times.\n";
print "While keeping your door you won ". $win_keep ." and lost ". $loose_keep ." times.\n";

I don't see the problem. Did your friend say exactly what was wrong with it?

Jufjufjuf
January 2nd, 2012, 06:28 PM
He said it doesn't account for which door the host opened... I'm like WTF? haha

TheMatrix
January 2nd, 2012, 09:06 PM
Then your friend is strange :rolleyes:
The $hostopened variable says all ;)