random 0 1 javascript code example

Example 1: random js

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

Example 2: nombre random js

// On renvoie un nombre aléatoire entre une valeur min (incluse) 
// et une valeur max (exclue)
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

Example 3: javascript random number in range

function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
}

Example 4: javascript random number between 0 and 10

Math.floor(Math.random() * 10);

Example 5: javascript random number in range

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}