xxxxxxxxxx
const randomNumber = getRandomNumberBetween(1,1000)
const getRandomNumberBetween = (min, max) => {
return Math.floor(Math.random() * (max - min + 1) + min)
}
xxxxxxxxxx
var min = 1;
var max = 90;
var stop = 6; //Number of numbers to extract
var numbers = [];
for (let i = 0; i < stop; i++) {
var n = Math.floor(Math.random() * max) + min;
var check = numbers.includes(n);
if(check === false) {
numbers.push(n);
} else {
while(check === true){
n = Math.floor(Math.random() * max) + min;
check = numbers.includes(n);
if(check === false){
numbers.push(n);
}
}
}
}
sort();
//Sort the array in ascending order
function sort() {
numbers.sort(function(a, b){return a-b});
document.getElementById("array_number").innerHTML = numbers.join(" - ");
}
xxxxxxxxxx
//math.random gives us numbers between 0 and 1
//when we multiply it on max-min we get numbers between 0 and (max-min)
//after that we add min both max and min so we have number between min and max
//function gives us random numbers between 2 numbers we will specify
const randomInt = (min, max) =>
Math.floor(Math.random() * (max - min) + 1) + min;
randomInt(min, max);
xxxxxxxxxx
/**
* 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;
}