Friday, September 26, 2025
HomeLanguagesJavascriptLodash _.random() Method

Lodash _.random() Method

The _.random() method is used to return a random number which 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: This method accepts three parameters as mentioned above and described below:

  • 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:

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
          
// Use of _.random method 
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:  

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// lower and upper value
let lower = 2; 
let upper = 11; 
    
// Printing 5 random values 
// in range 2 and 11 
for (let i = 0; i < 5; i++) { 
    console.log(_.random(lower, upper)); 
}


Output:

9
5
2
11
5
RELATED ARTICLES

Most Popular

Dominic
32321 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6690 POSTS0 COMMENTS
Nicole Veronica
11857 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11912 POSTS0 COMMENTS
Shaida Kate Naidoo
6802 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6761 POSTS0 COMMENTS
Umr Jansen
6768 POSTS0 COMMENTS