Lodash - random method



Syntax

_.random([lower=0], [upper=1], [floating])

Produces a random number between the inclusive lower and upper bounds. If only one argument is provided a number between 0 and the given number is returned. If floating is true, or either lower or upper are floats, a floating-point number is returned instead of an integer.

Arguments

  • [lower=0] (number) − The lower bound.

  • [upper=1] (number) − The upper bound.

  • [floating] (boolean) − Specify returning a floating-point number.

Output

  • (number) − Returns the random number.

Example

var _ = require('lodash');
var result = _.random( 1, 4);

console.log(result);

result = _.random(1, 4, true);
console.log(result);

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

2
2.486341743797542
lodash_number.htm
Advertisements