generate random int javascript code example

Example 1: js random number

var random;
var max = 8
function findRandom() {
  random = Math.floor(Math.random() * max) //Finds number between 0 - max
  console.log(random)
}
findRandom()

Example 2: generate random number javascript

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

Example 3: angular random number between 1 and 10

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

Example 4: javascript random

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

Example 5: generate random integer javascript

/**
 * Returns a random number between min (inclusive) and max (exclusive)
 */
function getRandomArbitrary(min, max) {
    return Math.random() * (max - min) + min;
}

/**
 * Returns a random integer between min (inclusive) and max (inclusive).
 * The value is no lower than min (or the next integer greater than min
 * if min isn't an integer) and no greater than max (or the next integer
 * lower than max if max isn't an integer).
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}