JavaScript - Generate random number(Integer) with in a range.
Recently I was working on Neoload script (Performance Testing) where I need to generate random number between 10 to 300.
Random number can be generated using random()
alert(Math.random()); // returns number like 0.98899988888
To generate integers with in the range, I have used following logic.
alert(randomXToY(10,300));
function randomXToY(minVal,maxVal,floatVal)
{
var randVal = minVal+(Math.random()*(maxVal-minVal));
return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}
minVal - Minimum value
maxVal - Maximum value
floatVal - (Optional) Floating point digits. Value is not defined, it will round to integer value.
---
Recently I was working on Neoload script (Performance Testing) where I need to generate random number between 10 to 300.
Random number can be generated using random()
alert(Math.random()); // returns number like 0.98899988888
To generate integers with in the range, I have used following logic.
alert(randomXToY(10,300));
function randomXToY(minVal,maxVal,floatVal)
{
var randVal = minVal+(Math.random()*(maxVal-minVal));
return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}
minVal - Minimum value
maxVal - Maximum value
floatVal - (Optional) Floating point digits. Value is not defined, it will round to integer value.
---
No comments:
Post a Comment