Created
September 1, 2020 16:10
-
-
Save brookjordan/b5e66cdec3d2e4ac1adca79dd0e69895 to your computer and use it in GitHub Desktop.
Test the birthday paradox for yourself
This file contains hidden or 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
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