Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 410
T.script Avatar asked Aug 17 '17 12:08

T.script


People also ask

How do I get 2 decimal places in JavaScript?

Use the toFixed() method to format a number to 2 decimal places, e.g. num. toFixed(2) . The toFixed method takes a parameter, representing how many digits should appear after the decimal and returns the result.

How do you generate a random number between two values in JavaScript?

The Complete Full-Stack JavaScript Course! To generate a random number, we use the Math. random() function. This method returns a floating-point number in the range 0 (inclusive) to 1 (exclusive). To generate random numbers in different range, we should define the minimum and maximum limits of the range.

How do you move a number to 2 decimal places?

Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.

How to create random numbers in JavaScript?

JavaScript Random 1 Math.random (). Math.random () always returns a number lower than 1. 2 JavaScript Random Integers. Math.random () used with Math.floor () can be used to return random integers. There is no... 3 A Proper Random Function. As you can see from the examples above, it might be a good idea to create a proper random... More ...

How to generate random number between 1 and 6 in Excel?

Task: generate random number between 1 and 6. Math.random () returns floating point number between 0 and 1 (like 0.344717274374 or 0.99341293123 for example), which we will use as a percentage, so Math.floor (Math.random () * 6) + 1 returns some percentage of 6 (max: 5, min: 0) and adds 1.

How do you create a random decimal number in Python?

This function takes no parameters and creates a random decimal number between 0 and 1. The returned value may be 0, but it will never be 1. You can use Math.random () to create whole numbers (integers) or numbers with decimals (floating point numbers).

How do you find the random number between two numbers?

Once we got the floored percentage of (difference + 1), we can add lower boundary to get the desired randomed number between 2 numbers. The logic formula for that will be: Math.floor (Math.random () * ( (up_boundary - low_boundary) + 1)) + 10.


2 Answers

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); 
like image 92
Observer Avatar answered Nov 15 '22 14:11

Observer


Multiply the original random number by 10^decimalPlaces, floor it, and then divde 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; } 
like image 42
clabe45 Avatar answered Nov 15 '22 14:11

clabe45