Generate Random Numbers
Examples in
JavaScript
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 between 0 and 1
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}