Random Number Generation using java script
While i searching for code in internet for random number generation in javascript. I fond this code. I hope it will usefull for some developers to integrate this code in their work. Try this code.
Get random number in javascript by using readymade function random():
alert(Math.random());
Function to generate random numbers between range 1 to N:
function randomToN(maxVal,floatVal)
{
var randVal = Math.random()*maxVal;
return typeof floatVal==’undefined’?Math.round(randVal):randVal.toFixed(floatVal);
}
In above function, you want to pass 2 arguments, First argument , “maxVal” Maximum number of value needed (N) and second argument, “floatVal” is optional which specifies number of digits after decimal point. If u did’t give floatVal means, it will return value as integer.
Function to return a random numbers in a range:
function randomXToY(minVal,maxVal,floatVal)
{
var randVal = minVal+(Math.random()*(maxVal-minVal));
return typeof floatVal==’undefined’?Math.round(randVal):randVal.toFixed(floatVal);
}
Above function needs 3 arguments, First srgument, “minVal” is minimum number to be started and “maxVal” is a second argument, range between which the random number has to be generated. 3rd argument “floatVal” is which specifies number of digits after decimal point. If u did’t give floatVal means, it will return value as integer.
Sorry for bad english.


Leave a Reply