Created
September 12, 2020 19:31
-
-
Save NikkyAI/180296576ef561f0229c625feb2766bd to your computer and use it in GitHub Desktop.
monty hall problem thing
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
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