Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math random numbers between 50 and 80

Tags:

javascript

I know there's several questions on this, but I'm struggling to find out how to use Math.random to get random numbers between two high integers?

So, for example, between 50 and 80. I thought this would work...

'left': Math.floor((Math.random() * 80) + 50) + '%'

Any ideas?

like image 627
John the Painter Avatar asked Sep 20 '13 16:09

John the Painter


People also ask

How do you calculate math random?

To create a random decimal number between two values (range), you can use the following formula: Math. random()*(b-a)+a; Where a is the smallest number and b is the largest number that you want to generate a random number for.

How do you generate random numbers between 0 and 100 in Java?

Generate a random number by calling the nextInt() method and passing the upper bound (100) to the method as a parameter. It will generate a number between 0 (inclusive) and 100 (exclusive). To make it between 1(inclusive) and 100 (inclusive), add 1 to the number returned by the method.

What is the most common number chosen from 1 to 100?

The most random two-digit number is 37, When groups of people are polled to pick a “random number between 1 and 100”, the most commonly chosen number is 37.

How do you generate unique random numbers?

In a column, use =RAND() formula to generate a set of random numbers between 0 and 1.


2 Answers

Assuming the range is inclusive on both ends:

function getRandomInt (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
like image 164
kayz1 Avatar answered Sep 19 '22 21:09

kayz1


You need to know the range of the random.

Between 50 and 80, the range is 30 (80 - 50 = 30), then you add 1.

Therefor, the random would look like this :

Math.floor(Math.random() * 31) + 50
like image 39
Karl-André Gagnon Avatar answered Sep 19 '22 21:09

Karl-André Gagnon