Math Random Numbers
Math.random() will create a decimal number between 0 and 1 (e.g: 0.833556767). We have to use the following procedure to convert the decimal number to a random number of use. Creating a random number less than a value:
To create a random number less than any value, we will have to use two functions, random and round of Math object. For an example we will try to create a random number less than 1000
var xx = Math.random();
var rnumber = Math.round(xx*1000);
In the above example,
- First a random decimal number was created using Math.random()
- Now we multiply it with the number below which we want random number to be.
- Pass is as the argumnet to Math.Round to remove the decimal points.
- If we would want a number below 100, we would have to use 100 instead of 1000.