random method javascript code example
Example 1: math.random javascript
Math.random()
Math.floor(Math.random() * 10)
Math.floor(Math.random() * 11)
function randomNum(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
Example 2: generate random number javascript
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
Example 3: js random generator
var options = ["Your options", "Another option!", "This is an option."];
var chosenOption = Math.floor(Math.random() * options.length);
console.log(options[option]);
Example 4: random number in js
Math.random() returns a random number in [0,1)