Generate a random float - Node.js Number

Node.js examples for Number:Random Number

Description

Generate a random float

Demo Code


/**// w ww  .j  a va2s .  co  m
 * Generate a random float
 *
 * @param  {number} minValue  Minimum boundary
 * @param  {number} maxValue  Maximum boundary
 * @param  {number} precision Precision
 * @return {number}           Generated float
 */
export default function randomFloat( minValue, maxValue, precision = 2 ) {
  return parseFloat( Math.min( minValue + ( Math.random() * ( maxValue - minValue ) ), maxValue ).toFixed( precision ) );
}

Related Tutorials