Skip to content

Instantly share code, notes, and snippets.

@NikkyAI
Created September 12, 2020 19:31
Show Gist options
  • Save NikkyAI/180296576ef561f0229c625feb2766bd to your computer and use it in GitHub Desktop.
Save NikkyAI/180296576ef561f0229c625feb2766bd to your computer and use it in GitHub Desktop.
monty hall problem thing
import kotlin.random.Random
val doorCount = 4
val values = List(doorCount-1) { false } + true
var stick = 0
var switch = 0
repeat(10000) {
val doors = values.shuffled()
val picked = Random.nextInt(doorCount)
val otherDoorIndices = (doors.indices - picked)
// open all but one of the other doors
val otherDoors = otherDoorIndices.map { it to doors[it] }.toMap().toMutableMap()
while(otherDoors.entries.size > 1) {
val smurfIndex = otherDoors.entries.find { (_,v) -> !v }!!.key
otherDoors.remove(smurfIndex)
}
// val otherDoorIndex = otherDoorIndices.find { doors[it] } ?: otherDoorIndices.random()
require(otherDoors.entries.size == 1)
val otherDoorIndex = otherDoors.entries.first().key
if(doors[picked]) {
stick++
} else if(doors[otherDoorIndex]) {
switch++
}
}
println("doors: $doorCount")
println("attempts: ${stick + switch}")
println("stick: $stick ratio: ${stick / (stick + switch * 1.0f)}")
println("switch: $switch ratio: ${switch / (stick + switch * 1.0f)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment