Skip to content

Instantly share code, notes, and snippets.

@brookjordan
Created September 1, 2020 16:10
Show Gist options
  • Save brookjordan/b5e66cdec3d2e4ac1adca79dd0e69895 to your computer and use it in GitHub Desktop.
Save brookjordan/b5e66cdec3d2e4ac1adca79dd0e69895 to your computer and use it in GitHub Desktop.
Test the birthday paradox for yourself
async function getChanceOfMatchingBirthday({
numberOfPeopleInTheRoom = 23,
howManyRoomsToAverageBetween = 1e5,
} = {}) {
let initDate = Date.now();
let testCount = 0;
let matchCount = 0;
while (testCount < howManyRoomsToAverageBetween) {
if (!(testCount % 5e3) && Date.now() >= initDate + 16) {
await new Promise((resolve) => setTimeout(resolve, 0));
initDate = Date.now();
}
matchCount += +Array.from({ length: numberOfPeopleInTheRoom }, () => Math.ceil(Math.random() * 365))
.some((num, i, arr) => arr.indexOf(num) !== i);
testCount += 1;
}
return matchCount / testCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment