Created
May 25, 2023 17:11
-
-
Save JWPapi/88eb125a32801d479bf6ddcdaa7bec07 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
const SIMULATIONS = 1000000; | |
let villainDeaths = 0; | |
for (let i = 0; i < SIMULATIONS; i++) { | |
let villainLives = 8; | |
let minionLives = [2, 6, 5]; | |
for (let j = 0; j < 5; j++) { | |
let targets = []; | |
if (villainLives > 0) { | |
targets.push(() => villainLives -= 2); | |
} | |
minionLives.forEach((lives, index) => { | |
if (lives > 0) { | |
targets.push(() => minionLives[index] -= 2); | |
} | |
}); | |
let randomTarget = targets[Math.floor(Math.random() * targets.length)]; | |
randomTarget(); | |
} | |
if (villainLives <= 0) { | |
villainDeaths++; | |
} | |
} | |
console.log(`The Villain died in ${villainDeaths} out of ${SIMULATIONS} simulations.`); | |
console.log(`Estimated probability: ${villainDeaths / SIMULATIONS}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment