Created
June 1, 2022 23:08
-
-
Save bluepnume/adf807bac9c825ffefe5839f30d2d70a 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 randomPercentile = () => { | |
return Math.floor(Math.random() * 100); | |
}; | |
const events = [ | |
{ | |
name: 'A', | |
percentage: 33 | |
}, | |
{ | |
name: 'B', | |
percentage: 33 | |
}, | |
{ | |
name: 'C', | |
percentage: 34 | |
} | |
]; | |
const results = {}; | |
for (let i = 0; i < 100_000; i++) { | |
for (event of events) { | |
if (randomPercentile() < event.percentage || event.name === 'C') { | |
results[event.name] ??= 0; | |
results[event.name]++; | |
break; | |
} | |
} | |
} | |
console.log(results); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment