Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bluepnume/adf807bac9c825ffefe5839f30d2d70a to your computer and use it in GitHub Desktop.
Save bluepnume/adf807bac9c825ffefe5839f30d2d70a to your computer and use it in GitHub Desktop.
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