Math Random Numbers

how to create a random number in javascript?
how to create a random number between two numbers e.g: 0 and 1000 in javascript?


Object: Math
Method: Random
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


<script language=javascript>
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.

Ask Questions

Ask Question