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
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;
🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Web β€Ί JavaScript β€Ί Reference β€Ί Global_Objects β€Ί Math β€Ί random
Math.random() - JavaScript | MDN
This example returns a random number between the specified values. The returned value is no lower than (and may possibly equal) min, and is less than (and not equal) max. ... This example returns a random integer between the specified values.
🌐
W3Schools
w3schools.com β€Ί js β€Ί js_random.asp
JavaScript Random
Multiplying by 10 gives a number from 0 up to but not including 10. Adding 1 shifts that range to 1 up to but not including 11. Math.floor() then rounds down, so you get an integer between 1 and 10. // Returns a random integer from 1 to 100 (both included): Math.floor(Math.random() * 100) + 1; Try it Yourself Β» Β· As you can see from the examples above, it might be a good idea to create a proper random function to use for all random integer purposes. This JavaScript function always returns a random integer between min (included) and max (excluded): function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min) ) + min; } Try it Yourself Β» Β·
🌐
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 - This is the simplest way to generate a random number within a range using Math.random(). ... let min = 10; let max = 20; let random = Math.floor(Math.random() * (max - min + 1)) + min; console.log(`Random number between ${min} and ${max}: ...
🌐
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 - However, generating a random number within a specific range requires some additional steps. ... let randomNumber = Math.random(); console.log(randomNumber); // Output: a floating-point number between 0 and 1
🌐
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):
🌐
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 - To get this random number, we multiply the difference by the random number we got from Math.random and we apply Math.round on the result to round the number to the nearest integer.
🌐
Josh W. Comeau
joshwcomeau.com β€Ί snippets β€Ί javascript β€Ί random
Generate a random number in a range in JavaScript β€’ Josh W. Comeau
For example, a random number between 10 and 20. This little helper lets us do that! ... This random function includes the lower bound, but excludes the upper bound. For example, random(10, 12) will grant either 10 or 11, but never 12. This was done intentionally, to match the behaviour of Math.random, as well as JavaScript methods like slice(opens in new tab). ... // Get a random number out of [10, 11, 12, 13] random(10, 14); // Get a random number from 1 to 100 (inclusive) random(1, 101); // Get a random number from -10 to 10 (inclusive) random(-10, 11);
Find elsewhere
🌐
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?

🌐
Vultr Docs
docs.vultr.com β€Ί javascript β€Ί examples β€Ί generate-a-random-number-between-two-numbers
JavaScript Program to Generate a Random Number Between Two Numbers | Vultr Docs
December 18, 2024 - Understand the Math.random() function in JavaScript, which returns a floating-point number between 0 (inclusive) and 1 (exclusive).
🌐
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 a powerful tool in JavaScript that generates a pseudo-random numberβ€”a number that seems random but is actually generated through a deterministic process. It returns a floating-point, or decimal, number between 0 (inclusive) and 1 (exclusive)....
🌐
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
Generating a random integer between X and Y, including Y, you must add a + 1 to the difference between max and min: (max - min + 1. Here’s the full code snippet for the between method: /** * Returns a random number between min (inclusive) and max (inclusive) */ function between(min, max) ...
🌐
Reddit
reddit.com β€Ί r/learnjavascript β€Ί generating random numbers within a range.
r/learnjavascript on Reddit: Generating random numbers within a range.
July 27, 2014 -

I'm a complete newbie not just to JavaScript but to programming altogether. And I mean, a newbie, no previous experience, just about finishing the codecademy's JS track right now and reading a couple of books atm. Codecademy's great fun but I get the feeling that being hold by a virtual hand while writing itsy bits of code is not how this is going to work, so I've decided to fiddle away at my own, obviously incredibly basic, little projects.

And for one of them I need a dice roll function which I'll be able to use for multiple types of dice (you know d4, d6, d8 etc). I came up with this:

function diceRoll(min, max) {
    return Math.floor(Math.random()* (max-min)) + min;
}

It's working, but can anyone tell me if it's the best option? What other ways are there to generate random numbers within a 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 - JavaScript's Math.random() is handy for generating random numbers between 0 and 1. If you need a random number in a different range, you'll need to use some simple math to map the random number to the desired range.
🌐
GitHub
gist.github.com β€Ί kerimdzhanov β€Ί 7529623
JavaScript: get a random number from a specific range Β· GitHub
/** * Get a random floating point ... {number} a random floating point number */ let randomFloat = (min, max) => Math.random() * (max - min) + min; /** * Get a random integer between `min` and `max`. * * @param {number} min - min ...
🌐
KIRUPA
kirupa.com β€Ί html5 β€Ί random_numbers_js.htm
Random Numbers in JavaScript
Once you do that, you will be able to generate all numbers within the range of your high and low numbers, 10 and 50 respectively, with equal frequency. Using Math.random() works as advertised. It gets us a random number between 0 and almost 1. What it doesn't give us is a random number that is cryptographically secure.
🌐
Keploy
keploy.io β€Ί home β€Ί community β€Ί how to generate random numbers in javascript
How to Generate Random Numbers in JavaScript | Keploy Blog
November 1, 2024 - Learn to generate random numbers, integers, Booleans in JavaScript for different scenarios, from basic to advanced techniques.
🌐
Stack Abuse
stackabuse.com β€Ί javascript-generate-random-number-in-range
JavaScript: Generate Random Number in Range
September 21, 2023 - Math.random() in JavaScript generates a floating-point (decimal) random number between 0 and 1 (inclusive of 0, but not 1). Let's check this out by calling: ... This is useful if you're dealing with percentages, as any value between 0 and 1, rounded to two decimal places, can be thought of ...
🌐
Codecademy
codecademy.com β€Ί forum_questions β€Ί 5020be4d3a51800002015ebe
Math.floor and Math.random | Codecademy
If you tried to use 40 and add 1, it would get you to 40, but it would also start at 1. If you want to test your randoms, substitute Math.random() with 0 and 0.99999 to get your (approximate) range.