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.
Discussions

Generate A Random Number Within A Range - How Does the Math Work?
Math.floor() is the floor function. Which means it returns the integer part of a number. In your example Math.floor(5.94) = 5 More on reddit.com
🌐 r/learnprogramming
8
0
February 4, 2022
Generate a Random Number Between Range
How come you subtract the min from the max instead of adding 1 to the min first? I thought addition came before subtraction Β· So, 1 is added to the range because the number is rounded down, and you do not want to exclude a potential case where the result is equal to max. min is added to the ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
May 18, 2020
Basic JavaScript - Generate Random Whole Numbers with JavaScript
Tell us what’s happening: The instructions for this lesson ask you to use the Math.floor(Math.random()) * N to generate and return a random whole number between 0 and 9 When I return Math.floor(Math.random()*9) it is not accepted. The error says " You should have multiplied the result of ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
April 18, 2023
Creating a random number within a defined range using JavaScript
How can I make JavaScript pick a random number from a specific set? I’m working on a little project and I need to get JavaScript to choose a number at random. But here’s the thing: I want it to pick from a certain group of numbers, not just any number. Let’s say I want it to pick a number ... More on community.latenode.com
🌐 community.latenode.com
0
November 29, 2024
🌐
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 Β» Β·
🌐
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?

🌐
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)....
🌐
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 ... provides a built-in method, Math.random(), which generates a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive)....
Find elsewhere
🌐
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 - If you need a random decimal (float) within a range, skip rounding. JavaScript Β· let min = 1.5; let max = 5.5; let random = Math.random() * (max - min) + min; console.log(`Random decimal between ${min} and ${max}: ${random}`); Output Β·
🌐
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);
🌐
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.
🌐
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.
🌐
freeCodeCamp
forum.freecodecamp.org β€Ί javascript
Basic JavaScript - Generate Random Whole Numbers with JavaScript - JavaScript - The freeCodeCamp Forum
April 18, 2023 - Tell us what’s happening: The instructions for this lesson ask you to use the Math.floor(Math.random()) * N to generate and return a random whole number between 0 and 9 When I return Math.floor(Math.random()*9) it is…
🌐
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).
🌐
Latenode
community.latenode.com β€Ί other questions β€Ί javascript
Creating a random number within a defined range using JavaScript - JavaScript - Latenode Official Community
November 29, 2024 - How can I make JavaScript pick a random number from a specific set? I’m working on a little project and I need to get JavaScript to choose a number at random. But here’s the thing: I want it to pick from a certain group of numbers, not just any number. Let’s say I want it to pick a number ...
🌐
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):
🌐
p5.js
p5js.org β€Ί reference β€Ί p5 β€Ί random
random
For example, calling random(['🦁', '🐯', '🐻']) returns either a lion, tiger, or bear emoji. The version of random() with two parameters returns a random number from a given range.
🌐
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) ...
🌐
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.
🌐
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 ...