Use a combination of JavaScript Math methods: random to generate a random value
between 0 and 1, which is then multiplied by 255.
var randomNumber = Math.floor(Math.random() * 255);
console.log(randomNumber);
The random method generates a random number between 0 and 1. To increase the range,
multiply the result by the upper end of the range of values you want. If you need a
random number with a higher lower end, such as a number between 5 and 10, multiply
the value from random by a number equal to the upper range, minus the lower range,
minus 1, and then add the lower range to the result:
var randomNumber = Math.floor(Math.random() * 6) + 5;
console.log(randomNumber);
The floor method rounds down the floating-point value to the nearest integer.
0 Comment(s)