Created
April 2, 2024 10:08
-
-
Save UniBreakfast/f8e30aa97d23e62f4b8e180a5880d4da to your computer and use it in GitHub Desktop.
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
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