I need to create a random number between 0.0200 and 0.120 using JavaScript. How would I do this?
-
7This 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
-
1The 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
3 Answers
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.
Hope it helps ^_^
- 1,740
- 1
- 10
- 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
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.
- 4,769
- 6
- 56
- 93
-
-
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
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.
- 1,097
- 9
- 12