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.

Great post. The random between function is pretty helpful.

hi good thank you master

Thanks Buddy!!!

Its really helpful, getting random number in JavaScript.

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);

}

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

http://jsbin.com/ihaqi/3/edit

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

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: http://jpaq.org/documentation/Math.randomIn%28%29/1.0/