Last active
February 13, 2019 10:22
-
-
Save deximat/bc55344143af3c2be4207a227231cdd6 to your computer and use it in GitHub Desktop.
Math problem from 21 movie - https://www.youtube.com/watch?v=8DMnAAvakh0
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
int count = 100000000; | |
int hits = 0; | |
Random random = new Random(); | |
for (int i = 0; i < count; i++) { | |
final List<Integer> doors = new ArrayList<>(); | |
doors.add(0); | |
doors.add(1); | |
doors.add(2); | |
Integer answer = random.nextInt(3); | |
Integer choosen = random.nextInt(3); | |
final List<Integer> notAnswerAndNotChoosen = new ArrayList<>(doors); | |
notAnswerAndNotChoosen.remove(answer); | |
notAnswerAndNotChoosen.remove(choosen); | |
Integer notAnswer = notAnswerAndNotChoosen.get(random.nextInt(notAnswerAndNotChoosen.size())); | |
doors.remove(notAnswer); | |
doors.remove(choosen); | |
if (random.nextBoolean()) { | |
choosen = doors.get(0); | |
} | |
if (choosen == answer) { | |
hits++; | |
} | |
} | |
log.debug("Probability: {}", (double) hits / count); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Turn off line 23. for original solution.