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;
🌐
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…
🌐
freeCodeCamp
forum.freecodecamp.org β€Ί guide
freeCodeCamp Challenge Guide: Generate Random Whole Numbers with JavaScript - Guide - The freeCodeCamp Forum
June 10, 2019 - Generate Random Whole Numbers with JavaScript Solutions Solution 1 (Click to Show/Hide) var randomNumberBetween0and19 = Math.floor(Math.random() * 20); function randomWholeNum() { // Only change code below this line. …
🌐
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 ...
🌐
Welcm Learning
welcm.uk β€Ί blog β€Ί how-to-create-a-random-whole-number-in-javascript
How to Create a Random Whole Number in JavaScript | Welcm Learning. Blog
As with Step 1, Math is the object and, in this case, trunc() is the method. ... β€œMake me a variable called number. Calculate its value for me by making a random number between 0 (inclusive) and 20 (exclusive) and then truncate it to remove the decimal points.” Β· This line will produce an integer (a whole number) between 0 (inclusive) and 19 (inclusive).
🌐
W3Schools
w3schools.com β€Ί js β€Ί js_random.asp
JavaScript Random
Example outputs: 0.0, 0.237, 0.9999, but never 1. Math.random() * 10 gives a range from 0 up to but not including 10. Example possible results: 0.0, 3.5, 9.99, etc. Math.floor() rounds a number down to the nearest whole integer: 3.5 becomes 3 Β·
Find elsewhere
🌐
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 - This function multiplies the difference between max and min by the result of Math.random(), then adds the min value. This scales the random number to your desired range. Generating a whole number (integer) within a range involves a similar approach but includes steps to round off the decimal:
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί generate-random-whole-numbers-within-a-range-javascript
How to Generate Random Whole Numbers within a Range using JavaScript Math.floor - Solved
November 28, 2019 - Quick Solution function randomRange(myMin, myMax) { return Math.floor(Math.random() * (myMax - myMin + 1) + myMin); } Code Explanation Math.random() generates our random number between 0 and β‰ˆ 0.9. Before multiplying it, it resolves the part ...
🌐
TutorialsPoint
tutorialspoint.com β€Ί How-to-generate-random-whole-numbers-in-JavaScript-in-a-specific-range
How to generate random whole numbers in JavaScript in a specific range?
To generate a random number, use the JavaScript Math.random() method. Here set the minimum and maximum value and generate a random number between them as in the following code βˆ’ Example
🌐
GitHub
github.com β€Ί Rafase282 β€Ί My-FreeCodeCamp-Code β€Ί wiki β€Ί Lesson-Generate-Random-Whole-Numbers-with-JavaScript
Lesson Generate Random Whole Numbers with JavaScript
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. ... Rafael J. Rodriguez edited this page ... It's great that we can create random decimal numbers, but it's even more useful if we lot more useful to generate a random whole number.
Author Β  Rafase282
🌐
CoreUI
coreui.io β€Ί answers β€Ί how-to-generate-a-random-integer-in-javascript
How to generate a random integer in JavaScript Β· CoreUI
September 26, 2025 - This method provides precise control over the range and ensures truly random distribution of whole numbers. Use Math.floor() with Math.random() to generate random integers within a specified range.
🌐
Vultr Docs
docs.vultr.com β€Ί javascript β€Ί standard-library β€Ί Math β€Ί random
JavaScript Math random() - Generate Random Number | Vultr Docs
November 29, 2024 - Use Math.random() along with Math.floor() to generate random integers within a specified range. Combine these functions to create random integers from a minimum (min) to a maximum (max) value.
🌐
Career Karma
careerkarma.com β€Ί blog β€Ί javascript β€Ί javascript random number: a complete guide
JavaScript Random Number: A Complete Guide
December 1, 2023 - We can use the Math.floor() function to round our random number up to become a random integer, and we can multiply that number to generate a larger one. Here’s an example of a random number generator function in JavaScript:
🌐
LaunchCode
education.launchcode.org β€Ί intro-to-professional-web-dev β€Ί appendices β€Ί math-method-examples β€Ί random-examples.html
Math.random Examples β€” Introduction to Professional Web Development in JavaScript documentation
If a random integer must be generated, the result of Math.random() can be manipulated with operators (+, -, *, /) and other Math methods. The trick to creating a random integer is to multiply Math.random() by a whole number and then round the result to remove the decimal portion.
🌐
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.
🌐
Quora
quora.com β€Ί How-do-I-generate-3-random-whole-numbers-within-a-given-range-in-JavaScript
How to generate 3 random whole numbers within a given range in JavaScript - Quora
Answer (1 of 3): Below function which accept two integers(low and high range value) and it returns the array of three numbers between the given range. function randomNumberGenerator (low, high) { var a = []; a.push(Math.floor(Math.random() * ...