Skip to content

Instantly share code, notes, and snippets.

@thomasnield
Last active September 23, 2018 20:31
Show Gist options
  • Select an option

  • Save thomasnield/7fe76d27a57afbea49939dc1879c9883 to your computer and use it in GitHub Desktop.

Select an option

Save thomasnield/7fe76d27a57afbea49939dc1879c9883 to your computer and use it in GitHub Desktop.
Monte Carlo Simulation of Monty Hall
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