I want to achieve a condition inside a for loop, which will get exponentially less likely to be true after every loop pass.
Here is a simplified version of my current, linear solution: every loop pass it is x+1 less likely that the random number === 0 and thus the probability per run is reduced.
for (let x = 0; x < 10; x++) {
if (getRandomInt(0,x) === 0) {
// do something
}
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
But I don't know how to change the condition so that the probability per loop pass becomes exponential and not linearly smaller as in my solution.
Anyone an idea? Thanks for your time!
Aucun commentaire:
Enregistrer un commentaire