# How to get a Random Float in a Range using JavaScript

Last updated: Mar 3, 2024
3 min

## #Get a Random Float in a Range in JavaScript

To get a random float in a range:

1. Use the `Math.random()` function to get a random number from `0` to `0.999..`.
2. Multiply the number by the result of subtracting the `min` value in the range from the `max` value.
3. Add the `min` value to the result.
index.js
```Copied!```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:

1. The `min` value in the range.
2. The `max` value in the range.
3. The number of decimals we want to round the number to.

If you don't want to round the random float to N decimal places, remove the call to the `toFixed()` method.

index.js
```Copied!```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.

index.js
```Copied!```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...
``````
We multiply the random number by the result of subtracting the `min` value from the `max` value.
index.js
```Copied!```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`.

The next step is to add the `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.

index.js
```Copied!```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.

index.js
```Copied!```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
``````

## #Get a Random Float in a Range using `lodash`

Alternatively, you can use the `lodash` library.

First, install the `lodash` module if you haven't already.

shell
```Copied!```# ๐๏ธ initialize a package.json file
npm init -y

npm install lodash
``````

Now you can import and use the lodash.random() method.

index.js
```Copied!```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:

NameDescription
`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