dimanche 21 novembre 2021

How to guarantee a certain number of random occurrences in a loop?

I have a program that simulates the rotation of a clock's hand and it randomly jumps 2x the normal rotation amount. I need to guarantee that in a full rotation, I get at least n jumps. How should I go about randomizing it but ensuring at least n jumps?

I thought about having a guaranteed jump every x iterations but then it's not random.

Here is the code of my rotation loop:

for (let i = 0; i < ticks; i++) {           
    // Move the clockhand by a position, sometimes by a greater amount 
    setTimeout(() => {
        let d = Math.random();
        jump = false;
        // Randomize and keep track of jumps
        if (d < (probabilty/100)) {
            jump = true;
        }
     }
}



Aucun commentaire:

Enregistrer un commentaire