There are some examples on the Mozilla Developer Network page:

/**
 * Returns a random number between min (inclusive) and max (exclusive)
 */
function getRandomArbitrary(min, max) {
    return Math.random() * (max - min) + min;
}

/**
 * Returns a random integer between min (inclusive) and max (inclusive).
 * The value is no lower than min (or the next integer greater than min
 * if min isn't an integer) and no greater than max (or the next integer
 * lower than max if max isn't an integer).
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

Here's the logic behind it. It's a simple rule of three:

Math.random() returns a Number between 0 (inclusive) and 1 (exclusive). So we have an interval like this:

[0 .................................... 1)

Now, we'd like a number between min (inclusive) and max (exclusive):

[0 .................................... 1)
[min .................................. max)

We can use the Math.random to get the correspondent in the [min, max) interval. But, first we should factor a little bit the problem by subtracting min from the second interval:

[0 .................................... 1)
[min - min ............................ max - min)

This gives:

[0 .................................... 1)
[0 .................................... max - min)

We may now apply Math.random and then calculate the correspondent. Let's choose a random number:

                Math.random()
                    |
[0 .................................... 1)
[0 .................................... max - min)
                    |
                    x (what we need)

So, in order to find x, we would do:

x = Math.random() * (max - min);

Don't forget to add min back, so that we get a number in the [min, max) interval:

x = Math.random() * (max - min) + min;

That was the first function from MDN. The second one, returns an integer between min and max, both inclusive.

Now for getting integers, you could use round, ceil or floor.

You could use Math.round(Math.random() * (max - min)) + min, this however gives a non-even distribution. Both, min and max only have approximately half the chance to roll:

min...min+0.5...min+1...min+1.5   ...    max-0.5....max
β””β”€β”€β”€β”¬β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€ ... β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”¬β”€β”€β”˜   ← Math.round()
   min          min+1                          max

With max excluded from the interval, it has an even less chance to roll than min.

With Math.floor(Math.random() * (max - min +1)) + min you have a perfectly even distribution.

 min...  min+1...    ...      max-1... max....   (max+1 is excluded from interval)
β””β”€β”€β”€β”¬β”€β”€β”€β”˜β””β”€β”€β”€β”¬β”€β”€β”€β”˜β””β”€β”€β”€ ... β”˜β””β”€β”€β”€β”¬β”€β”€β”€β”˜β””β”€β”€β”€β”¬β”€β”€β”€β”˜   ← Math.floor()
   min     min+1               max-1    max

You can't use ceil() and -1 in that equation because max now had a slightly less chance to roll, but you can roll the (unwanted) min-1 result too.

Answer from IonuΘ› G. Stan on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Web β€Ί JavaScript β€Ί Reference β€Ί Global_Objects β€Ί Math β€Ί random
Math.random() - JavaScript | MDN
The Math.random() static method returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range β€” which you can then scale to your desired range. The implementation selects the initial seed to the random ...
🌐
SitePoint
sitepoint.com β€Ί blog β€Ί javascript β€Ί how to generate random numbers in javascript with math.random()
Generating Random Numbers in JavaScript with Math.random()
November 7, 2024 - Math.random() is a built-in JavaScript function that generates a pseudo-random floating-point number in the range from 0 (inclusive) to 1 (exclusive).
Top answer
1 of 16
4922

There are some examples on the Mozilla Developer Network page:

/**
 * Returns a random number between min (inclusive) and max (exclusive)
 */
function getRandomArbitrary(min, max) {
    return Math.random() * (max - min) + min;
}

/**
 * Returns a random integer between min (inclusive) and max (inclusive).
 * The value is no lower than min (or the next integer greater than min
 * if min isn't an integer) and no greater than max (or the next integer
 * lower than max if max isn't an integer).
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

Here's the logic behind it. It's a simple rule of three:

Math.random() returns a Number between 0 (inclusive) and 1 (exclusive). So we have an interval like this:

[0 .................................... 1)

Now, we'd like a number between min (inclusive) and max (exclusive):

[0 .................................... 1)
[min .................................. max)

We can use the Math.random to get the correspondent in the [min, max) interval. But, first we should factor a little bit the problem by subtracting min from the second interval:

[0 .................................... 1)
[min - min ............................ max - min)

This gives:

[0 .................................... 1)
[0 .................................... max - min)

We may now apply Math.random and then calculate the correspondent. Let's choose a random number:

                Math.random()
                    |
[0 .................................... 1)
[0 .................................... max - min)
                    |
                    x (what we need)

So, in order to find x, we would do:

x = Math.random() * (max - min);

Don't forget to add min back, so that we get a number in the [min, max) interval:

x = Math.random() * (max - min) + min;

That was the first function from MDN. The second one, returns an integer between min and max, both inclusive.

Now for getting integers, you could use round, ceil or floor.

You could use Math.round(Math.random() * (max - min)) + min, this however gives a non-even distribution. Both, min and max only have approximately half the chance to roll:

min...min+0.5...min+1...min+1.5   ...    max-0.5....max
β””β”€β”€β”€β”¬β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€ ... β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”¬β”€β”€β”˜   ← Math.round()
   min          min+1                          max

With max excluded from the interval, it has an even less chance to roll than min.

With Math.floor(Math.random() * (max - min +1)) + min you have a perfectly even distribution.

 min...  min+1...    ...      max-1... max....   (max+1 is excluded from interval)
β””β”€β”€β”€β”¬β”€β”€β”€β”˜β””β”€β”€β”€β”¬β”€β”€β”€β”˜β””β”€β”€β”€ ... β”˜β””β”€β”€β”€β”¬β”€β”€β”€β”˜β””β”€β”€β”€β”¬β”€β”€β”€β”˜   ← Math.floor()
   min     min+1               max-1    max

You can't use ceil() and -1 in that equation because max now had a slightly less chance to roll, but you can roll the (unwanted) min-1 result too.

2 of 16
657
var randomnumber = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
🌐
W3Schools
w3schools.com β€Ί js β€Ί js_random.asp
JavaScript Random
Math.random() * 10 gives a range from 0 up to but not including 10.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί javascript β€Ί how-to-generate-random-number-in-given-range-using-javascript
Generate Random Number in Given Range Using JavaScript - GeeksforGeeks
July 11, 2025 - Math.random() generates a random decimal between 0 (inclusive) and 1 (exclusive). Multiply the result by (max - min + 1) to scale it to the desired range, then add min to shift it to the correct starting value.
🌐
Reddit
reddit.com β€Ί r/learnprogramming β€Ί generate a random number within a range - how does the math work?
r/learnprogramming on Reddit: Generate A Random Number Within A Range - How Does the Math Work?
February 4, 2022 -

Hi all,

I'm learning JS through FCC and it's going okay so far. I'm not a very mathematically inclined person, however, so I struggle with some concepts.

I'm struggling to understand how the following code works;

Math.floor(Math.random() * (max - min + 1)) + min

I tried writing out an example on paper and used 10 and 5 as max and min respectively.

Math.floor(Math.random() * (10 - 5 + 1)) + 5

wouldn't this mean that I can get numbers higher than the max though? Say I get a randomly generated 0.99; well 0.99 * (6) + 5 = 10.94 which is greater than my max of 10?

Am I doing this wrong?

🌐
Josh W. Comeau
joshwcomeau.com β€Ί snippets β€Ί javascript β€Ί random
Generate a random number in a range in JavaScript β€’ Josh W. Comeau
// Between 0 and 0.999999 (inclusive) const initialRandom = Math.random(); // Multiply it by our DELTA, 3. // Will be between 0 and 2.999999 (inclusive) const multiplied = initialRandom * DELTA; // Round it down using Math.floor.
Find elsewhere
🌐
Medium
medium.com β€Ί @ryan_forrester_ β€Ί random-number-in-a-range-in-javascript-464dfeddf8d4
Random Number in a Range in JavaScript | by ryan | Medium
September 11, 2024 - This guide will provide you with ... Math.random(), which generates a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive)....
🌐
Vultr Docs
docs.vultr.com β€Ί javascript β€Ί standard-library β€Ί Math β€Ί random
JavaScript Math random() - Generate Random Number | Vultr Docs
November 29, 2024 - The Math.random() method in JavaScript is an essential tool for generating random numbers. This function returns a floating-point, pseudo-random number in the range from 0 (inclusive) to 1 (exclusive), which you can then scale to your desired range.
🌐
30 Seconds of Code
30secondsofcode.org β€Ί home β€Ί javascript β€Ί math β€Ί random number or integer in range
Generate a random number or integer in a range with JavaScript - 30 seconds of code
March 12, 2024 - The simplest use case you'll come across is to generate a random number in a specified range. All you need to do is multiply the result of Math.random() by the difference between the maximum and minimum values, and then add the minimum value.
🌐
Codecademy
codecademy.com β€Ί forum_questions β€Ί 5020be4d3a51800002015ebe
Math.floor and Math.random | Codecademy
So, when you use Math.floor(Math.random() * 10), it gets you an integer (a whole number) between (and including) 0 to 9. If you add 1 to it like Math.floor(Math.random() * 10) + 1, that range changes to 1 to 10.
🌐
Udacity
udacity.com β€Ί blog β€Ί 2021 β€Ί 04 β€Ί javascript-random-numbers.html
Creating Javascript Random Numbers with Math.random() | Udacity
September 27, 2022 - In the previous examples, Math.random() could never create a number at the very top of a specified range. If you wanted a number between 0 and 5, for example, you could get 0-4, but never 5.
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί generate-random-number-within-a-range-in-javascript
How to Generate a Random Number within Certain a Range in JavaScript
December 6, 2022 - The random method returns a random floating number between 0 and 1. Here's a code example: Math.random() // 0.26636355538480383 Math.random() // 0.6272624945940997 Math.random() // 0.05992852707853347
🌐
Educative
educative.io β€Ί answers β€Ί how-to-generate-a-random-number-between-a-range-in-javascript
How to generate a random number between a range in JavaScript
In JavaScript, the Math.random function will generate a random floating point number between 0 and 1(0 is included 1 is excluded):
🌐
KIRUPA
kirupa.com β€Ί html5 β€Ί random_numbers_js.htm
Random Numbers in JavaScript
That's all there is to generating a random number that falls within a range that you specify. In JavaScript, Math.random() returns a number greater than or equal to 0 but less than 1:
🌐
Programiz
programiz.com β€Ί javascript β€Ί library β€Ί math β€Ί random
JavaScript Math random()
Here, we can see that the random value produced by Math.random() is scaled by a factor of the difference of the numbers. Then it is added to the smaller number to produce a random number between the given range.
🌐
CoreUI
coreui.io β€Ί blog β€Ί how-to-generate-a-random-number-in-javascript
Javascript Random - How to Generate a Random Number in JavaScript? Β· CoreUI
April 16, 2024 - Math.random() is designed to return a number from 0 up to but not including 1 to ensure a uniform distribution of numbers over the range 0 to less than 1.
🌐
Futurestud.io
futurestud.io β€Ί tutorials β€Ί generate-a-random-number-in-range-with-javascript-node-js
Generate a Random Number in Range With JavaScript/Node.js
This between method accepts two arguments: min and max. Calculate a random number between the min and max values like this:use Math.random() to generate a random number, multiply this random number with the difference of min and max and ultimately add min to it.
🌐
Droidscript
droidscript.org β€Ί javascript β€Ί Global_Objects β€Ί Math β€Ί random.html
Using Math.random()
The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.