random number injs code example
Example 1: random number javascript
/*
The Math.random() function returns a floating-point, pseudo-random
number in the range 0 to less than 1 (inclusive of 0, but not 1)
with approximately uniform distribution over that range — which you
can then scale to your desired range. The implementation selects the
initial seed to the random number generation algorithm; it cannot
be chosen or reset by the user.
*/
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
console.log(getRandomInt(3));
// expected output: 0, 1 or 2
console.log(getRandomInt(1));
// expected output: 0
console.log(Math.random());
// expected output: a number from 0 to <1
Example 2: how to generate a random number in javascript
// min value of the random number
var min = 5;
// max value of the random number
var max = 25;
// generate the random number
var rdm = (Math.random() * (max - min)) + min
// generate the random number without "."
var rdm = Math.round((Math.random() * (max - min)) + min)