Random Number Complete Function

Benchmark created on


Setup

function isNumber(num){
    return (typeof num === 'number' && !isNaN(num));
}

function toFixed(value,precision, roundingMethod = 'round') 
{
    roundingMethod = roundingMethod.toLowerCase()

    if(isNaN(precision)){
        throw new Error("precision is NaN");
    }
    if(isNaN(value)){
        throw new Error("value is NaN");
    }
    if(precision<0) precision = 0

    let negative = (value<0);
    
    value = Math.abs(value);
    
    let fixer = Math.pow(10,precision);

    if(roundingMethod == 'floor')  value = Math.floor(value * fixer) / fixer; 
    else if(roundingMethod == 'ceil')  value = Math.ceil(value * fixer) / fixer;
    else  value = Math.round(value * fixer) / fixer;

    return (negative ? (-value) : value);
}

function findPrecisionStr(num) {
    if (!isFinite(num)) return 0;
    if (typeof num !== "number") num = +num; 
    if (isNaN(num)) return 0;
    
    num = num.toString();
    let afterDot = num.match(/\.(.+?)(?=e|$)/)
    let afterE = num.match(/e[+-](.+)/)
    
    afterDot = afterDot ? afterDot[1].length : 0
    afterE = afterE ? +afterE[1] : 0

    return (afterDot + afterE)
}

function randNum(min,max, precision = true)
{
    if(isNaN(min) || isNaN(max))
    {
        throw new Error("min or max is NaN");
    }
    if(min == max)  return max;

    if(precision === true){
        let minP = findPrecision(min)
        let maxP = findPrecision(max)
        if(minP==16.5) minP = 16;
        if(maxP==16.5) maxP = 16;
        precision = maxP > minP ? maxP : minP;

        return toFixed(((Math.random() * ((max+1)-min)) + min), precision);
    }

    if(isNumber(precision)){
        return toFixed(((Math.random() * ((max+1)-min)) + min), precision);
    }
    else{
        return ((Math.random() * ((max+1)-min)) + min);
    }
}

let opts = [
  [1, 2, 2],
  [0, 12, false],
  [-8, 4, true],
  [-1.34, 2.945, true],
  [-20, -5, null],
  [2.8, 0, 2],
  [1.4858, 6.45, true],
  [9.857203, 8.857203, 3],
  [-2.3455, 2.3455, true],
  [3.89, 3.456, 1],
  [0, 0, true]
]

Test runner

Ready to run.

Testing in
TestOps/sec
"Complex" rand num
for(let i = 0, b=0; i < opts.length; ++i){
	b = randNum(opts[0],opts[1],opts[2])
}
ready
Normal rand num
for(let i = 0, b=0; i < opts.length; ++i){
	b = ((Math.random() * ((opts[0]+1)-opts[1])) 
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.