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");
}
}
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");
}
}