Last updated: Mar 3, 2024
Reading timeยท3 min

To get a random float in a range:
Math.random() function to get a random number from 0 to
0.999...min value in the range
from the max value.min value to the result.function getRandomFloat(min, max, decimals) { const str = (Math.random() * (max - min) + min).toFixed( decimals, ); return parseFloat(str); } console.log(getRandomFloat(1.5, 3.5, 2)); // ๐๏ธ 2.18 console.log(getRandomFloat(1.5, 3.5, 3)); // ๐๏ธ 1.648 console.log(getRandomFloat(1.5, 3.5, 4)); // ๐๏ธ 2.334

We created a reusable function that returns a random float in a range and takes 3 parameters:
min value in the range.max value in the range.If you don't want to round the random float to N decimal places, remove the call
to the toFixed() method.
function getRandomFloat(min, max) { return Math.random() * (max - min) + min; } console.log(getRandomFloat(1.5, 3.5, 2)); // ๐๏ธ 2.2339321273754646 console.log(getRandomFloat(1.5, 3.5, 3)); // ๐๏ธ 2.245253958251207 console.log(getRandomFloat(1.5, 3.5, 4)); // ๐๏ธ 1.5363023338742452
The
Math.random()
function returns a number from 0 up to 1.
The number could be 0, but it can never be 1.
Here are some examples.
console.log(Math.random()); // ๐๏ธ 0.4234243... console.log(Math.random()); // ๐๏ธ 0.8124353... console.log(Math.random()); // ๐๏ธ 0.0043243... // ๐๏ธ returns a number from 0 up to 1.999... console.log(Math.random() * (3.5 - 1.5)); // ๐๏ธ 1.328438... console.log(Math.random() * (3.5 - 1.5)); // ๐๏ธ 0.803458... console.log(Math.random() * (3.5 - 1.5)); // ๐๏ธ 0.734543...
min value from the max value.function getRandomFloat(min, max, decimals) { const str = (Math.random() * (max - min) + min).toFixed(decimals); return parseFloat(str); } console.log(getRandomFloat(1.5, 3.5, 2)); // ๐๏ธ 2.18 console.log(getRandomFloat(1.5, 3.5, 3)); // ๐๏ธ 1.648 console.log(getRandomFloat(1.5, 3.5, 4)); // ๐๏ธ 2.334
In the examples, the max value in the range is 3.5 and the min value is
1.5. Subtracting the min from the max returns 2.
When multiplied with Math.random(), the result could be from 0 (if
Math.random returns 0) up to 1.999.
min value (1.5) to the result to get a float in the range.The Number.toFixed method formats a number to N decimal places.
const num = 2.2339321273754646; console.log(num.toFixed(2)); // ๐๏ธ "2.23" console.log(num.toFixed(3)); // ๐๏ธ "2.234" console.log(num.toFixed(4)); // ๐๏ธ "2.2339"
The toFixed() method returns a string, so the last step is to use the
parseFloat() function to
parse the string into a float.
If you don't want to format the number to N decimal places, simply remove the
call to the toFixed() method.
Then you can also remove the call to the parseFloat() function because the
value is already of type number.
function getRandomFloat(min, max) { return Math.random() * (max - min) + min; } console.log(getRandomFloat(1.5, 3.5, 2)); // ๐๏ธ 2.2339321273754646 console.log(getRandomFloat(1.5, 3.5, 3)); // ๐๏ธ 2.245253958251207 console.log(getRandomFloat(1.5, 3.5, 4)); // ๐๏ธ 1.5363023338742452
lodashAlternatively, you can use the lodash library.
First, install the lodash module if you haven't already.
# ๐๏ธ initialize a package.json file npm init -y npm install lodash
Now you can import and use the lodash.random() method.
import _ from 'lodash'; console.log(_.random(1.5, 3.5, true)); // ๐๏ธ 1.6857339558747713 console.log(_.random(1.5, 3.5, true)); // ๐๏ธ 2.955297568340108 console.log(_.random(1.5, 3.5, true)); // ๐๏ธ 3.0891939168066593

The lodash.random() method takes the following 3 parameters:
| Name | Description |
|---|---|
lower | an inclusive lower range. The value defaults to 0 |
upper | an inclusive upper range. The value defaults to 1 |
floating | A boolean value for whether the random number should be a float |
You can learn more about the related topics by checking out the following tutorials: