I'm trying to find a way to optimize this
Let's say i have this set of chances:
let chances = {
a: 0.80,
b: 0.15,
c: 0.05
}
and I want to run these chances X times and store how many of each that was obtained
of course the simplest way is to do an iteration with those chances, for example like this:
let results = { a: 0, b: 0, c: 0 };
for (let i = 0; i < iterations; i++) {
let rnd = Math.random();
let chanceSum = 0;
for (let c in chances) {
if (rnd <= chances[c] + chanceSum) {
results[c]++;
}
else chanceSum += chances[c];
}
}
then results
would have how many of each was obtained
the problem comes with a BIG number of iterations: clearly doing it like this for thousands of millions of times cause massive performance issues
of course, multiplying the chance with the amount of iterations and then rounding the number is a quick way to solve it, but of course it's not truly random
my idea is to obtain the results without actually doing the iterations, how could i achieve this? My best idea is using the normal distribution somehow but i'm not sure how to apply it