T.script
T.script

Reputation: 327

Generate a Random Number between 2 values to 2 decimals places in Javascript

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

Upvotes: 30

Views: 62158

Answers (7)

clabe45
clabe45

Reputation: 2454

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;
}

Upvotes: 24

oldboy
oldboy

Reputation: 5954

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.

Upvotes: 0

Lu&#237;s Marques
Lu&#237;s Marques

Reputation: 1

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));

Upvotes: 0

Nickofthyme
Nickofthyme

Reputation: 4327

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

Upvotes: 3

vinay k hegde
vinay k hegde

Reputation: 253

You can use below code.

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

Upvotes: 3

Diego Ven&#226;ncio
Diego Ven&#226;ncio

Reputation: 6007

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

Upvotes: 1

Observer
Observer

Reputation: 3706

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);

Upvotes: 27

Related Questions