Created
October 18, 2013 10:03
-
-
Save Kyly/7039343 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.PrintWriter; | |
public class ThreeDoors { | |
// Holds items | |
private int cIndex;//Car index | |
private int pIndex;//Guest choice | |
private ThreeDoors() { | |
PrintWriter outSw = null; | |
PrintWriter outSt = null; | |
try { | |
outSw = new PrintWriter(new FileOutputStream("Switches.csv")); | |
outSt = new PrintWriter(new FileOutputStream("Stays.csv")); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
System.exit(0); | |
} | |
//CSV heads | |
outSw.println("index, Prob: If guest always switches"); | |
outSt.println("index, Prob: If guest always stays"); | |
// Play style sum of results | |
int swSum = 0, stSum = 0; | |
double swProp = 0, stProp = 0; | |
for (int i = 1; i <= 100000; i++) { | |
// Play round | |
play(); | |
if (cIndex == pIndex) {//Wins on a stay | |
stSum++; | |
stProp = (double) stSum / i; | |
} else {//Wins on switch | |
swSum++; | |
swProp = (double) swSum / i; | |
} | |
outSw.println(i +","+ swProp); | |
outSt.println(i + "," + stProp); | |
} | |
outSw.close(); | |
outSt.close(); | |
} | |
private void play() { | |
// Car is put in 0-2 | |
cIndex = randomDoor(); | |
// Guest pick a door | |
pIndex = randomDoor(); | |
} | |
public static void main(String[] args) { | |
ThreeDoors td = new ThreeDoors(); | |
System.out.print("Done"); | |
} | |
public static int randomDoor() { | |
return (int) (Math.random() * 10 % 3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a real plain example of the Monty Hall problem.