Created
September 23, 2016 14:00
-
-
Save blizzy78/217780d1fd3597c53092aa9b770e020e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public class MontyHall { | |
| public static void main(String... args) { | |
| boolean[] doors = new boolean[3]; | |
| int wins = 0; | |
| int losses = 0; | |
| int tries = 10000000; | |
| for (int i = 1; i <= tries; i++) { | |
| // fill doors | |
| for (int j = 0; j < doors.length; j++) { | |
| doors[j] = false; | |
| } | |
| int winIndex = (int) (Math.random() * doors.length); | |
| doors[winIndex] = true; | |
| // pick random door | |
| int pickIndex = (int) (Math.random() * doors.length); | |
| // find remaining losing door | |
| int loseIndex = -1; | |
| for (int j = 0; j < doors.length; j++) { | |
| if ((j != pickIndex) && !doors[j]) { | |
| loseIndex = j; | |
| break; | |
| } | |
| } | |
| // change pick | |
| for (int j = 0; j < doors.length; j++) { | |
| if ((j != pickIndex) && (j != loseIndex)) { | |
| pickIndex = j; | |
| break; | |
| } | |
| } | |
| if (doors[pickIndex]) { | |
| wins++; | |
| } else { | |
| losses++; | |
| } | |
| } | |
| float winPercent = (float) wins / (float) tries; | |
| System.err.printf("wins: %.2f%%", winPercent * 100f).println(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment