Created
March 4, 2021 11:35
-
-
Save krmgns/5beed5ed67d5358b05c4243998662b3c to your computer and use it in GitHub Desktop.
Birthday paradox.
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
// https://betterexplained.com/examples/birthday/birthday.html | |
function calc(people) { | |
let ret = { | |
days: 365, | |
people: people | |
} | |
ret.combinations = ret.people * (ret.people - 1) / 2 | |
ret.chance = (ret.days - 1) / ret.days | |
ret.expected = 1 - (ret.chance ** ret.combinations) | |
ret.percent = ret.expected * 100 | |
return ret | |
} | |
let people = 100, percent, min_percent = 50.0 | |
for (let p = 1; p <= people; p++) { | |
percent = calc(p).percent | |
if (percent >= min_percent) { | |
people = p | |
break | |
} | |
} | |
//=> Person: 23, Percent: %50.05 | |
console.log("Person: %s, Percent: %%%s", people, percent.toFixed(2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment