Hi I would like to generate a random number between -x and x in JavaScript. This is what I have :
function randomInt(nb){
let absoluteVal = Math.ceil(Math.random()*nb)
let sign = Math.floor(Math.random()*2)
return sign == 1 ? absoluteVal*(-1) : absoluteVal;
}
console.log(randomInt(4))
It works but it is rather inelegant. I was wondering if somebody knew a better solution. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire