// Get random number between two numbers const random_number = _.random(1, 100); console.log(random_number);
Random number is: 75
The above code output will be different when you run the code. We are generating a random number between 1 to 100 using the code example.
To get a random number between a lower value and an upper value you can use the _.random() function. It takes one required parameter upper value. If you have not passed the lower value, it will consider the lower value 0.
The third parameter is the floating-point number. It is false by default but if you want your random number in floating type then you can pass it to true.
_.random(lower_value, upper_value, floating)
// random number between 0 and 10 const n1 = _.random(10); console.log(n1); // random number between 2 to 20 as float value const n2 = _.random(2, 20, true); console.log(n2); // random number between 1.7 to 10.4 as float value const n3 = _.random(1.7, 10.4); console.log(n3)
7 11.817919421376114 2.1816681956885513
Below is an example to generate a random number between 1 to 10
// get random interger value from 1 to 10 const result = _.random(1, 10); console.log(result); // get random float value between 1 to 10 const float_random = _.random(1, 10, true); console.log(float_random);