All files / random number.ts

100% Statements 8/8
100% Branches 10/10
100% Functions 1/1
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 159x         38x 36x 2x     34x   34x    
import { isNumber } from '../assert/is-number';
 
/**
 * Generate a random number >= min and <= max
 */
export function randomNumber(min = 0, max: number = Number.MAX_SAFE_INTEGER, fractionDigits?: number): number {
  if (!isNumber(min) || !isNumber(max)) {
    throw new Error('randomNumber must have min and max arguments');
  }
 
  const result = (Math.random() * (Math.abs(max - min))) + min;
 
  return fractionDigits ? parseFloat(result.toFixed(fractionDigits)) : Math.round(result);
}