Q:

javascript get random integer in given range

const randomFloat = (min, max) => Math.random() * (max - min) + min;
4
function getRandomNumberBetween(min,max){
    return Math.floor(Math.random()*(max-min+1)+min);
}

//usage example: getRandomNumberBetween(20,400); 
25
const min = 1;
const max = 4;
const intNumber = Math.floor(Math.random() * (max - min)) + min;
console.log(intNumber); //> 1, 2, 3
1
const randomInteger = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
3
const randomArrayInRange = (min, max, n) => Array.from({ length: n }, () => Math.floor(Math.random() * (max - min + 1)) + min);

// Example
randomArrayInRange(1, 100, 10); 
2
let object = random(0, 50);
// making "object" a random number between 0 and 50.

console.log(object);
// printing object in the console
0

New to Communities?

Join the community