🌐
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 ...
🌐
W3Schools
w3schools.com β€Ί js β€Ί js_random.asp
JavaScript Random
Math.random() returns a number from 0 (inclusive) up to but not including 1.
Discussions

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
Generating random whole numbers in JavaScript in a specific range - Stack Overflow
How can I generate random whole numbers between two specified variables in JavaScript, e.g. x = 4 and y = 8 would output any of 4, 5, 6, 7, 8? More on stackoverflow.com
🌐 stackoverflow.com
Generating random numbers within a range.
I'd go with the following: function diceRoll(n, sides) { var total = 0; for(var i = 0; i < n; i++) { total += Math.floor(Math.random()*sides) + 1; } return total; } The point of this approach is to get a similar distribution pattern to rolling physical dice. If you're just getting a random value between two numbers, each number within the range has an equal chance of being selected. However, when rolling multiple dice in real life, your chances of getting mid-range numbers is higher than your chances of getting fringe numbers. By giving different weapons different types of dice rolls, you can have weapons that do more reliable damage (5d4) vs weapons that are a little more of a gamble (1d20). More on reddit.com
🌐 r/learnjavascript
8
4
July 27, 2014
JavaScript generate random number except some values - Stack Overflow
Actually, we do not need to use contains(random) with a while loop. To simplify the question, let's see what happens if we only have one excluding value. We can split the result to 2 parts. Then the number of possible values is range-1. If the random number is less than the excluded value, ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί javascript β€Ί how-to-generate-a-random-number-in-javascript
How to Generate a Random Number in JavaScript? - GeeksforGeeks
July 23, 2025 - The Math.random()method in JavaScript is a foundational function for generating pseudo-random floating-point numbers between 0 (inclusive) and 1 (exclusive).
🌐
Udacity
udacity.com β€Ί blog β€Ί 2021 β€Ί 04 β€Ί javascript-random-numbers.html
Creating Javascript Random Numbers with Math.random() | Udacity
September 27, 2022 - Javascript is a full programming language, able to make complex mathematical calculations. Learn how you can create random numbers with Math.random()
🌐
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.
🌐
GitHub
github.com β€Ί ckknight β€Ί random-js
GitHub - ckknight/random-js: A mathematically correct random number generator library for JavaScript. Β· GitHub
A mathematically correct random number generator library for JavaScript. - ckknight/random-js
Author Β  ckknight
🌐
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
🌐
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…
🌐
KIRUPA
kirupa.com β€Ί html5 β€Ί random_numbers_js.htm
Random Numbers in JavaScript
That's all there is to generating a random number that falls within a range that you specify. In JavaScript, Math.random() returns a number greater than or equal to 0 but less than 1:
🌐
Medium
mavtipi.medium.com β€Ί how-to-generate-unique-random-numbers-in-a-specified-range-in-javascript-80bf1a545ae7
How to generate unique random numbers in a specified range in Javascript | by Mav Tipi | Medium
March 23, 2020 - Cool. In Ruby this would be as simple as random(16), but in JavaScript we have to write this function ourselves. And we just did! One second though. What I wanted was a number between one and fifteen, not zero and fifteen.
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;
🌐
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) ...
🌐
Team Treehouse
teamtreehouse.com β€Ί library β€Ί javascript-numbers β€Ί create-a-random-number
Create a Random Number (How To) | JavaScript Numbers | Treehouse
JavaScript lets you create random numbers, which you can use to add variety and surprise in your programs. You generate a random number in JavaScript via a method named Math.random().
Published Β  February 18, 2020
🌐
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?

🌐
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 - Learn how to use Math.random to generate random numbers in JavaScript and create random colors, letters, strings, phrases, passwords, & more.
🌐
Codebubb
codebubb.com β€Ί posts β€Ί javascript-random-number-generator
How to code a Javascript Random Number Generator Β· codebubb
November 1, 2023 - So the basics of generating random numbers in JavaScript is to use the Math.random() function which is something that is built-in to the JavaScript language.