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 ...
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() used with Math.floor() can be used to return random integers. There is no such thing as JavaScript integers. We are talking about numbers with no decimals here. // Return a random integer from 0 to 9 (both included): ...
🌐
Reddit
reddit.com β€Ί r/learnjavascript β€Ί generate random integer numbers
r/learnjavascript on Reddit: Generate random INTEGER numbers
October 22, 2021 -

Hi does anyone know how to generate random integer numbers using JavaScript. I tried using YouTube and it suggested that the β€œ math.floor β€œ & math.random ?tag was the way to go but I still don’t really understand it

UPDATE: This is the code I wrote but I don’t get any numbers generated and Vs code doesn’t report any issues with it.

Function Integer(min,max){ Let start=max-man +1 Let step2= math.random()*start; Let result=math.floor(step2) +min; Return result; } Integer(0,19);

🌐
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) ...
🌐
Math.js
mathjs.org β€Ί docs β€Ί reference β€Ί functions β€Ί randomInt.html
math.js | an extensive math library for JavaScript and Node.js
Return a random integer number larger or equal to min and smaller than max using a uniform distribution. math.randomInt() // generate either 0 or 1, randomly math.randomInt(max) // generate a random integer between 0 and max math.randomInt(min, max) // generate a random integer between min ...
🌐
W3Schools
w3schools.com β€Ί jsref β€Ί jsref_random.asp
JavaScript Math random() Method
The Math.random() method returns a random floating point number between 0 (inclusive) and 1 (exclusive).
Find elsewhere
🌐
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.
🌐
Vultr Docs
docs.vultr.com β€Ί javascript β€Ί standard-library β€Ί Math β€Ί random
JavaScript Math random() - Generate Random Number | Vultr Docs
November 29, 2024 - Combine these functions to create random integers from a minimum (min) to a maximum (max) value. javascript Copy Β· let min = 10; let max = 50; let randomInteger = Math.floor(Math.random() * (max - min + 1)) + min; console.log(randomInteger); ...
🌐
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…
🌐
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 - JavaScript’s built-in Math.random() ... than 1, simply multiply the result by a scale factor. To generate a random integer, use the Math.floor function ......
🌐
Udacity
udacity.com β€Ί blog β€Ί 2021 β€Ί 04 β€Ί javascript-random-numbers.html
Creating Javascript Random Numbers with Math.random() | Udacity
September 27, 2022 - Creating a pseudo-random integer is a little more difficult; you must use the function Math.floor() to round your computed value down to the nearest integer. 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. There may be a need to generate random numbers in a specific range that doesn’t start with 0, for example. Fortunately, there are simple Javascript functions that programmers can create to make pseudo-random numbers more manageable.
🌐
Programiz
programiz.com β€Ί javascript β€Ί library β€Ί math β€Ί random
JavaScript Math random()
// Generating random integer in range [x, y] // Both values are inclusive Β· function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } // random int between 5 and 10 var random_num = getRandomInt(5, 10); ...
🌐
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 MAX, 4. // Will be between 0 and 3.999999 (inclusive) const multiplied = initialRandom * MAX; // Round it down using Math.floor.
🌐
W3Resource
w3resource.com β€Ί javascript-exercises β€Ί javascript-math-exercise-4.php
JavaScript Math: Generate a random integer - w3resource
Write a JavaScript function that generates a random integer using Math.random() and rounds the result using bitwise operators.
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί javascript-random-number-how-to-generate-a-random-number-in-js
JavaScript Random Number – How to Generate a Random Number in JS
August 3, 2022 - The general syntax for the Math object methods is the following: ... Let's go over some examples of how to implement those methods. If you want to want to round a number to its nearest integer, use the Math.round() method:
🌐
Droidscript
droidscript.org β€Ί javascript β€Ί Global_Objects β€Ί Math β€Ί random.html
Using Math.random()
function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } // Returns a random integer between min (included) and max (included) // Using Math.round() will give you a non-uniform distribution!