Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
You need to generate a random number, between 0 and 255.
Use a combination of JavaScript Math methods: random to generate a random value between 0 and 1, which is then multiplied by 255, and floor to truncate the number.
var randomNumber = Math.floor(Math.random() * 255);
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;
The floor method rounds down the floating-point value to the nearest integer.