Lodash _.random() method is used to return a random number that is in the range provided to the function. If floating is true, or either lower or upper are floats, a floating-point number is returned instead of an integer.
Syntax:
_.random([lower = 0], [upper = 1], [floating]);
Parameters:
- lower: This parameter holds the lower bound.
- upper: This parameter holds the upper bound.
- floating: This parameter specifies returning a floating-point number.
Return Value:
This method returns the random number.
Example 1: In this example, we are printing the random number using the lodash _.random() method.
Javascript
const _ = require( "lodash" );
console.log(_.random(10));
console.log(_.random(10, 12));
console.log(_.random(10, 12));
console.log(_.random(10.3, 12.5));
|
Output:
3
11
10
12.000118273018167
Example 2: In this example, we are printing the random number using the lodash _.random() method.
Javascript
const _ = require( "lodash" );
let lower = 2;
let upper = 11;
for (let i = 0; i < 5; i++) {
console.log(_.random(lower, upper));
}
|
Output:
9
5
2
11
5
Last Updated :
31 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...