Created
October 25, 2017 08:10
-
-
Save polyglotpiglet/aa91de9957260debb50fe0b87e826b8b 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
| import scala.util.Random | |
| object MonthHall extends App { | |
| val numberOfIterations = 1000000 | |
| val numberOfWins = (0 until numberOfIterations).count(_ => { | |
| val winningDoor = Random.nextInt(3) | |
| val chosenDoor = Random.nextInt(3) | |
| val openedDoor = chosenDoor - winningDoor match { | |
| case 0 => (Random.nextInt(2) + 1 + winningDoor) % 3 | |
| case _ => (0 until 3).filter(i => i != winningDoor && i != chosenDoor)(0) | |
| } | |
| val switchedDoor = (0 until 3).filter(i => i != chosenDoor && i != openedDoor)(0) | |
| switchedDoor == winningDoor | |
| }) | |
| val percentageOfWins: Double = numberOfWins * 1.0 / numberOfIterations * 100 | |
| print("Switching makes you win : " + percentageOfWins + "% of the time!" ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment