Skip to content

Instantly share code, notes, and snippets.

@JWPapi
Created May 25, 2023 17:11
Show Gist options
  • Save JWPapi/88eb125a32801d479bf6ddcdaa7bec07 to your computer and use it in GitHub Desktop.
Save JWPapi/88eb125a32801d479bf6ddcdaa7bec07 to your computer and use it in GitHub Desktop.
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