73

I need to create a random number between 0.0200 and 0.120 using JavaScript. How would I do this?

I specifically am interested in floating point numbers, not whole numbers. The question linked (Generating random whole numbers in JavaScript in a specific range) specifically asks for "Generating random whole numbers", so even if some of the answers to the linked question specify how to do it for floats, it is not clear that is what they are doing. This question is clearly looking for a random number between floats and so is not a duplicate.

danieljames
  • 825
  • 1
  • 9
  • 15
  • 10
    This is *not* a duplicate of https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range. This question is specifically asking about floating point numbers, the other is whole numbers. The answers are not compatible since they use Math.floor(). – Ted Bigham Aug 06 '18 at 20:57
  • @TedBigham, the first example in the linked duplicate does not use floor. – Stephen Rauch Aug 07 '18 at 01:02
  • 1
    The linked answer that does not use floor also only gives an inclusive/exclusive answer. This question is ambiguous and based on the accepted answer they were looking for inclusive/inclusive, which also justifies non-duplicate status. – Ted Bigham Aug 07 '18 at 04:18
  • Given the accepted answer the asker was looking for random numbers with a fixed number of decimals, so that makes the duplicate reference valid. There is no relevant difference in working with integers or decimals with a fixed number of digits. If you need to, just multiply everything with 1000, use the duplicate, then divide by 1000 again. This should remain closed as duplicate. – trincot Sep 24 '22 at 13:13

3 Answers3

146

You could use

(Math.random() * (0.120 - 0.0200) + 0.0200).toFixed(4)

toFixed(n) is used to convert a number into a string, keeping only the "n" decimals.

starball
  • 20,030
  • 7
  • 43
  • 238
Alesanco
  • 1,840
  • 1
  • 11
  • 13
  • 18
    +1. For those that were curious, I calculated the standard deviation on this function to ensure that it has equal distribution of "randomness" - [view the results](https://gist.github.com/naomik/6030653). These kinds of things are [interesting to me](http://stackoverflow.com/questions/9407892/how-to-generate-random-sha1-hash-to-use-as-id-in-node-js/14869745#14869745). – Mulan Jul 18 '13 at 16:14
  • 4
    Throw a parseFloat( ) around that thing and it's perfect for me. – Ted Bigham Aug 06 '18 at 21:21
24

Here you are:

function generateRandomNumber() {
    var min = 0.0200,
        max = 0.120,
        highlightedNumber = Math.random() * (max - min) + min;

    alert(highlightedNumber);
};

generateRandomNumber();

I understand your real question — you do not know how to get a random number between floating numbers, right? By the way, the answer is passed before.

To play with the code, just click here to jsFiddle.

Update

To get the four first numbers of your decimal, use .toFixed(3) method. I've performed an example here, on jsFiddle.

Guilherme Oderdenge
  • 4,935
  • 6
  • 61
  • 96
  • Is it possible to round, only showing say 3 decimal places? I thought .toFixed would work but it hasn't. – danieljames Jul 18 '13 at 15:08
  • I've got it now I used `highlightedNumber = (Math.random() * (max - min) + min).toFixed(4);` – danieljames Jul 18 '13 at 15:11
  • I made an update. @Edit: Oh, sorry. I didn't saw the accepted answer. – Guilherme Oderdenge Jul 18 '13 at 15:14
  • For those that were curious, I calculated the standard deviation on this function to ensure that it has equal distribution of "randomness" - [view the results](https://gist.github.com/naomik/6030653). These kinds of things are [interesting to me](http://stackoverflow.com/questions/9407892/how-to-generate-random-sha1-hash-to-use-as-id-in-node-js/14869745#14869745). – Mulan Jul 18 '13 at 16:14
10

If you're looking to generate that and other random numbers or things, I'd suggest taking a look the Chance library. It provides a nice abstraction layer so you don't have to fiddle with Math.random() and write your own.

chance.floating({min: 0.02, max: 0.12});

Full disclosure: I'm the author so I'm a bit biased :)

Also, if this is the only random thing you need to generate or it's client-side where size is a real issue, I'd suggest just using one of the suggestions above. Not worth a few Kb where a couple lines will do.

Victor Quinn
  • 1,107
  • 9
  • 12