Comment obtenir un nombre aléatoire entre deux nombres

Apprendre à obtenir un nombre aléatoire entre deux nombres en JavaScript.

Nombre aléatoire

La fonction JavaScript suivante retourne toujours un nombre aléatoire compris entre la valeur minimale (incluse) et la valeur maximale (exclue) :

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min) ) + min;
}

Essayer vous-même

La fonction JavaScript suivante retourne toujours un nombre aléatoire compris entre les valeurs minimales et maximales (les deux incluses) :

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1) ) + min;
}

Essayer vous-même

Pages liées

Tutoriel :JavaScript aléatoire