Last active
September 23, 2018 20:31
-
-
Save thomasnield/7fe76d27a57afbea49939dc1879c9883 to your computer and use it in GitHub Desktop.
Monte Carlo Simulation of Monty Hall
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
| import java.util.concurrent.ThreadLocalRandom | |
| /** | |
| * Monty Hall Problem Simulation | |
| */ | |
| fun randomDoor() = ThreadLocalRandom.current().nextInt(0,3) | |
| // how many trials do you want to run? | |
| val trialCount = 10000 | |
| var stayWins = 0 | |
| var switchWins = 0 | |
| repeat(trialCount) { | |
| val prizeDoor = randomDoor() | |
| val selectedDoor = randomDoor() | |
| val openedDoor = (0..2).shuffled().first { d -> d != selectedDoor && d != prizeDoor } | |
| val switchedDoor = (0..2).shuffled().first { d -> d != selectedDoor && d != openedDoor } | |
| if (selectedDoor == prizeDoor) stayWins++ | |
| if (switchedDoor == prizeDoor) switchWins++ | |
| } | |
| println("STAY WIN RATE: ${stayWins.toDouble() / trialCount.toDouble()}") | |
| println("SWITCH WIN RATE: ${switchWins.toDouble() / trialCount.toDouble()}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment