Getting random number between range of two numbers in JavaScript

Advertisement

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.

Popularity: 9% [?]

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Related Posts

» Display random number in random way using JavaScript
» Textbox to accept only numbers (digits) using jquery
» Ip address validation in PHP using regular expression
» Solving Floating point number precision lost problem in PHP

4 Comments on “Getting random number between range of two numbers in JavaScript”

  • David wrote on 23 December, 2008, 17:43

    Great post. The random between function is pretty helpful.

  • sohbet wrote on 2 July, 2009, 13:22

    hi good thank you master

  • Data Outsourcing India wrote on 31 July, 2009, 15:29

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

Trackbacks

  1. pligg.com

Write a Comment