Skip to content

Instantly share code, notes, and snippets.

@UniBreakfast
Created April 2, 2024 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UniBreakfast/f8e30aa97d23e62f4b8e180a5880d4da to your computer and use it in GitHub Desktop.
Save UniBreakfast/f8e30aa97d23e62f4b8e180a5880d4da to your computer and use it in GitHub Desktop.
proveWithStats(100000)
function proveWithStats(steps) {
const winStats = {change: 0, keep: 0}
for (let i = 0; i < steps/2; i++) {
playRound('keep', winStats)
playRound('change', winStats)
}
return winStats
}
function playRound(strategy, stats) {
const doors = setUpDoors()
let choice = rnd(3)
const openDoor = openOneDoor(doors, choice)
if (strategy == 'change') choice = change(choice, openDoor)
const result = doors[choice]
stats[strategy] += result
}
function rnd(limit) {
return Math.floor(Math.random() * limit)
}
function setUpDoors() {
const doors = [0, 0, 0]
doors[rnd(3)] = 1
return doors
}
function openOneDoor(doors, choice) {
const availableDoors = []
for (const i in doors) {
if (i != choice && doors[i] == 0){
availableDoors.push(i)
}
}
return availableDoors[rnd(availableDoors.length)]
}
function change(choice, openDoor) {
for (let i = 0; i < 3; i++){
if (i != choice && i != openDoor){
return i
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment