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 ...
🌐
Math.js
mathjs.org β€Ί docs β€Ί reference β€Ί functions β€Ί random.html
math.js
math.random() // returns a random number between 0 and 1 math.random(100) // returns a random number between 0 and 100 math.random(30, 40) // returns a random number between 30 and 40 math.random([2, 3]) // returns a 2x3 matrix with random numbers between 0 and 1
Discussions

I need help understanding the math.random example in event module - Learn - Mozilla Discourse
in the first example of the math.random in the event module of JS, there is const btn = document.querySelector('button'); function random(number) { return Math.floor(Math.random() * (number+1)); } btn.onclick = function() { const rndCol = 'rgb(' + random(255) + ',' + random(255) + ',' + random(255) ... More on discourse.mozilla.org
🌐 discourse.mozilla.org
0
October 29, 2021
Generating random whole numbers in JavaScript in a specific range - Stack Overflow
I've created a JSFiddle if anyone ... method: jsfiddle.net/F9UTG/1 2013-06-05T13:56:07.693Z+00:00 ... IonuΘ› G. Stan Β· IonuΘ› G. Stan Over a year ago Β· @JackFrost yeah, that's right. You're not dumb, you're just learning :) 2016-01-27T14:19:58.993Z+00:00 ... This question is old, but understanding this answer took me way too much time O.o, I think expanding math.random on next JavaScript ... More on stackoverflow.com
🌐 stackoverflow.com
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
How bad is Math.random()?
For most implementations, Math.random() is sufficient. For instance, when it's used to generate random numbers for game play. It would not be suitable for cryptographic applications. The primary criticism is that it's deterministic, and not "truly random". Some alternatives are Web Crypto API and Random.js . More on reddit.com
🌐 r/learnjavascript
30
12
February 24, 2023
🌐
W3Schools
w3schools.com β€Ί JS β€Ί β€Ί js_random.asp
W3Schools.com
Math.random() returns a floating-point number between 0 (inclusive) and 1 (exclusive).
🌐
Mozilla Discourse
discourse.mozilla.org β€Ί mdn β€Ί learn
I need help understanding the math.random example in event module - Learn - Mozilla Discourse
October 29, 2021 - in the first example of the math.random in the event module of JS, there is const btn = document.querySelector('button'); function random(number) { return Math.floor(Math.random() * (number+1)); } btn.onclick = function() { const rndCol = 'rgb(' ...
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;
🌐
Udacity
udacity.com β€Ί blog β€Ί 2021 β€Ί 04 β€Ί javascript-random-numbers.html
Creating Javascript Random Numbers with Math.random() | Udacity
September 27, 2022 - So, to create a random number between 0 and 10: ... Math.random() is a useful function, but on its own it doesn’t give programmers an easy way to generate pseudo-random numbers for specific conditions.
Find elsewhere
🌐
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 - At the time of writing, all the major browsers currently use the xorshift128+ algorithm in the background to generate a pseudo-random number. To use it, simply enter Math.random() and it will return a pseudo-random floating point decimal number between 0 (inclusive) and 1 (exclusive):
🌐
GitHub
github.com β€Ί ckknight β€Ί random-js
ckknight/random-js: A mathematically correct ...
A mathematically correct random number generator library for JavaScript. - ckknight/random-js
Starred by 616 users
Forked by 51 users
Languages Β  TypeScript 94.8% | JavaScript 5.2%
🌐
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?

🌐
GeeksforGeeks
geeksforgeeks.org β€Ί javascript β€Ί javascript-math-random-method
JavaScript Math random() Method - GeeksforGeeks
July 15, 2024 - JS Tutorial Β· Web Tutorial Β· ... Updated : 15 Jul, 2024 Β· The JavaScript Math.random() function gives you a random number between 0 and just under 1. You can use this number as a base to get random numbers within any range ...
🌐
DEV Community
dev.to β€Ί jesterxl β€Ί seeing-javascript-mathrandom-not-be-so-random-1235
Seeing JavaScript Math.random Not Be So Random - DEV Community
February 5, 2023 - I hope that the frequencies my JS generates can demonstrate that the distribution of random numbers between 1 and 6 is fairly even, rather than being biased. Although the PRNG in use cannot be truly random, it does a good enough job. I think this is basically what your Monte Carlo simulation tells you, too. It's not saying you'll get 3.5, it's saying the average is 3.5. ... Not sure why Math.random eventually settling around the theoretical mean would mean it is not random?
🌐
npm
npmjs.com β€Ί package β€Ί random
random - npm
July 12, 2025 - Seedable random number generator supporting many common distributions. Defaults to Math.random as its underlying pseudorandom number generator.
      Β» npm install random
    
Published Β  Jul 12, 2025
Version Β  5.4.1
Author Β  Travis Fischer
🌐
React
react.dev β€Ί learn β€Ί rendering-lists
Rendering Lists – React
Similarly, do not generate keys on the fly, e.g. with key={Math.random()}. This will cause keys to never match up between renders, leading to all your components and DOM being recreated every time. Not only is this slow, but it will also lose any user input inside the list items.
🌐
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 β€Ί javascript-math-random-method-explained
JavaScript Math.random() Method Explained
January 25, 2020 - The Math.random() method will return a floating point (decimal) number greater than or equal to 0 and less than (but never equal to) 1. In other words 0 <= x < 1.
🌐
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 - Vue.js Β· 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.