30

I want to generate a random number between 1 and 10 up to 2 decimal places,

I'm currently using this below to generate my numbers,

var randomnum = Math.floor(Math.random() * (10.00 - 1.00 + 1.00)) + 1.00;

Ultimately, I would like to know how to generate numbers like:

1.66

5.86

8.34

In the format: var randomnum = then the code

sidenote: I don't remember why I previously had generated my numbers like that but remember something about Math.random generating numbers to 8 decimal places.

Thank you for the help! :)

Ps: I've seen a lot of posts about waiting to round down or up generated numbers and haven't found one wanting to generate them straight out.

UPDATE: I want a number value not a string that looks like a number

5
  • 1
    Numbers in JavaScript have the precision they have; it doesn't make a lot of sense to try and limit the precision to two decimal places. What you could do is generate integers between 1 and 1000 and then divide them by 100, but due to the nature of binary floating point you'll still end up with some "fuzz" in the fractional parts of the values. Commented Aug 17, 2017 at 12:49
  • Possible duplicate of Get a random number between 0.0200 and 0.120 (float numbers) Commented Aug 17, 2017 at 12:57
  • 1
    You can convert it using parseFloat.. What's the bog deal? Commented Aug 17, 2017 at 13:04
  • The link I provided has solution to create random decimal numbers which is your requirement. So you just have to convert the string using parseFloat(). That's all you have to do. Commented Aug 17, 2017 at 13:06
  • how would I do this? Commented Aug 17, 2017 at 13:08

7 Answers 7

27

You were very close, what you need is not to work with decimal numbers as min and max. Let's have max = 1000 and min = 100, so after your Math.floor you will need to divide by 100:

var randomnum = Math.floor(Math.random() * (1000 - 100) + 100) / 100;

Or if you want to work with decimals:

var precision = 100; // 2 decimals
var randomnum = Math.floor(Math.random() * (10 * precision - 1 * precision) + 1 * precision) / (1*precision);
Sign up to request clarification or add additional context in comments.

3 Comments

I got this to work with my website best so thank you, "Close but no cigar"
If i wanted to change this equation: is it? var randomnum = Math.floor(Math.random() * (max - min) + min) / 100;
100 is your precision of 2 decimal places. See updated post
24

Multiply the original random number by 10^decimalPlaces, floor it, and then divide by 10^decimalPlaces. For instance:

floor(8.885729840652472 * 100) / 100  // 8.88

function genRand(min, max, decimalPlaces) {  
    var rand = Math.random()*(max-min) + min;
    var power = Math.pow(10, decimalPlaces);
    return Math.floor(rand*power) / power;
}

for (var i=0; i<20; i++) {
  document.write(genRand(0, 10, 2) + "<br>");
}

Edit in response to comments:
For an inclusive floating-point random function (using this answer):

function genRand(min, max, decimalPlaces) {  
    var rand = Math.random() < 0.5 ? ((1-Math.random()) * (max-min) + min) : (Math.random() * (max-min) + min);  // could be min or max or anything in between
    var power = Math.pow(10, decimalPlaces);
    return Math.floor(rand*power) / power;
}

8 Comments

This is nice, but genRand(2, 4, 1) will never return 4.0. You'll need to add a modifier at the end depending on max (if max<4, mod=2.5e-16; if max<16, mod=1e-15, etc.).
@thdoan Yes, the upper bound is exclusive. Can you elaborate on your way to make it inclusive?
clabe45, I was about to submit my own solution until I came across this solution, so I'm going with it (why reinvent the wheel, right? :)).
@thdoan Thanks, I don't know how that really differs from my solution though, because I don't think the Math.min is needed
clabe45, the difference is the one I linked to goes to 4.0 :).
|
3

You can use below code.

var randomnum = (Math.random() * (10.00 - 1.00 + 1.00) + 1.00).toFixed(2);

Comments

3

Just to simplify things visually, mathematically and functionally based on the examples above.

function randomNumberGenerator(min = 0, max = 1, fractionDigits = 0, inclusive = true) {
  const precision = Math.pow(10, Math.max(fractionDigits, 0));
  const scaledMax = max * precision;
  const scaledMin = min * precision;
  const offset = inclusive ? 1 : 0;
  const num = Math.floor(Math.random() * (scaledMax - scaledMin + offset)) + scaledMin;

  return num / precision;
};

The Math.max protects against negative decimal places from fractionDigits

Comments

1

A more way with typescript/angular example:

getRandom(min: number, max: number) {
  return Math.random() * (max - min) + min;
}

console.log('inserting min and max values: ', this.getRandom(-10.0, -10.9).toPrecision(4));

getting random values between -10.0 and -10.9

Comments

0

A simple javascript example to generate a random number with precision:

function genRand(min, max, decimalPlaces) {
    return (Math.random() * (max - min) + min).toFixed(decimalPlaces) * 1;
}

console.log(genRand(-5, 5, 2));

Comments

0

Here is another way not yet mentioned, which will generate a random number between 1 and 9.99...

(Math.random() * 8 + 1 + Math.random()).toFixed(2)

console.log((Math.random() * 8 + 1 + Math.random()).toFixed(2))

Whether this is the most practical approach is another question altogether.

1 Comment

So this returns a string. You can check by using typeof. To make it work, wrap a parseFloat() around it so the output becomes a number.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.