How to get a Random Float in a Range using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# Table of Contents

  1. Get a Random Float in a Range in JavaScript
  2. Get a Random Float in a Range using lodash

# 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
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

get random float in range

The code for this article is available on GitHub

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
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
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
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
The code for this article is available on GitHub

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
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
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
# ๐Ÿ‘‡๏ธ initialize a package.json file npm init -y npm install lodash

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

index.js
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

get random float in range using lodash

The code for this article is available on GitHub

The lodash.random() method takes the following 3 parameters:

NameDescription
loweran inclusive lower range. The value defaults to 0
upperan inclusive upper range. The value defaults to 1
floatingA boolean value for whether the random number should be a float

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev