Illustrated below is a concise example where the script generates a list of random numbers ranging from 0 to 10 (you have the option to alter the range by adjusting the values for min/max in the getRandomInt(min, max)
function).
The argument in getNumbers(times)
specifies how many times you wish to select a number from the pool.
It's worth noting that this example may select a number multiple times on different interactions (since this requirement was not mentioned in the original question).
(function() {
var getRandomInt = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
var getNumbers = function(times) {
for (var i = 0; i < times; i++) {
console.log(getRandomInt(0, 100));
}
};
console.log('---------- Selecting 35 numbers randomly');
getNumbers(35);
console.log('---------- Selecting 25 numbers randomly');
getNumbers(25);
console.log('---------- Selecting 10 numbers randomly');
getNumbers(10);
})();
The modified version below only selects unique numbers (as per your request in the comment):
(function() {
var usedNumbers = [];
var getRandomInt = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
var getNumbers = function(times) {
for (var i = 0; i < times; i++) {
var number = getRandomInt(0, 100);
if (usedNumbers.indexOf(number) === -1) {
usedNumbers.push(number);
console.log(number);
}
}
};
console.log('---------- Selecting 35 unique numbers');
getNumbers(35);
console.log('---------- Selecting 25 unique numbers');
getNumbers(25);
console.log('---------- Selecting 10 unique numbers');
getNumbers(10);
})();