Getting random number between range of two numbers in JavaScript

Yesterday, Steve asked me how can we have random number function in JavaScript like rand() function of PHP where the programmer can specify the range of two numbers within which we need the random number. Today, I’m going to share two functions in JavaScript, In first function , you can specify the number and the function generate the random number between 1 and N. In another JavaScript function, you can specify range of two numbers between which you’ve to get a random number. Furthermore, there is optional last parameter in these function for decimal places, if provided, will return the random number with the decimal places specified in the third parameter.

Getting random number in JavaScript

Getting random number is very easy you can use JavaScript function random() of Math object to get the random number between 0 and 1. For example, above JavaScript statement returns a random number between 0 and 1.

alert(Math.random()); // returns number like 0.3871769046200184

JavaScript function to get random number between 1 and N

//function to get random number from 1 to n
function randomToN(maxVal,floatVal)
   var randVal = Math.random()*maxVal;
   return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);

As, you can see in the above JavaScript function, there are two parameters. One for the maximum value(N) up to which random number have to be generated. The second parameter is optional which specifies number of digits after decimal point.If not provided, this function returns integer.

JavaScript function to get random number between a range

//function to get random number upto m
function randomXToY(minVal,maxVal,floatVal)
  var randVal = minVal+(Math.random()*(maxVal-minVal));
  return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);

The above JavaScript funciton accepts three parameters.The first and second parameter is mandatory while the third is optional. The first and second parameter specifies the range between which the random number has to be generated. The thir parameter is optional which specifies number of floating point digits, if not provided, the above JavaScript function returns integer random number.

8 thoughts on “Getting random number between range of two numbers in JavaScript

  1. David

    Great post. The random between function is pretty helpful.

  2. hi good thank you master

  3. Thanks Buddy!!!
    Its really helpful, getting random number in JavaScript.

  4. Nate

    The rounding causes a favoring in the middle of the range, not an even distribution:
    For example: randomXToY(0, 2) will return 1 half of the time and 0 a 1/4th of the time and 2 also just 1/4 of the time.

    maybe a better way is to use floor and you have to up the range by one unit of granularity.
    function randomMinToMax(min, max)
    var range = max – min + 1;
    return Math.floor(Math.random()*range+min);

  5. Nate

    here is a link to to see the histogram, give it a try:

  6. Thank you so much!!! now I can generate a range of years. I generated a random year between 2011 – 2112

  7. Darrel

    I have started to use jPaq and found that they have a function called Math.randomIn. This function seems to offer the same functionality. I found it here:

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>