mercredi 25 mai 2022

How do I prevent a duplicate random number when using setInterval? (javascript)

I've been wanting to generate random numbers through a function and then use setInterval to repeat that function and give me a new number every time.

function randNum(prevNum) { 
    let randNum = Math.round(Math.random() * 12)     //choose a number between 0 and 12 
    while (randNum == prevNum){                //if the chosen number is the same as prevNum, choose another random number
        randColor = Math.round(Math.random() * 12) 
    }
    prevNum = randColor            //assign current value to parameter 
    return (prevNum);              //return parameter
}


prevNum = 0,
prevNum = setInterval (randNum, 1000, prevNum)     //assign returned parameter to current variable, then go back into the function with the new parameter value.

also using node so semicolons might be missing.




Aucun commentaire:

Enregistrer un commentaire