🌐
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
math - JavaScript Random Number - Stack Overflow
This maybe more mathematical question than programming. In JS I wanted to a function that returns a random integer number in an interval lets say 1-6 and this is what I found: // Returns a random 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
🌐
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()
🌐
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…
🌐
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
Starred by 616 users
Forked by 51 users
Languages Β  TypeScript 94.8% | JavaScript 5.2%
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 - 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) ...
🌐
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).
🌐
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.
🌐
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:
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;
Top answer
1 of 4
6

Assuming you already understand the behaviour of Math.floor and Math.random, here's the rest step by step:

  • Math.random() ↝ a random number between 0 (inclusive) and 1 (exclusive)
  • Math.random() * max ↝ a random number between 0 (inclusive) and max (exclusive)
  • Math.floor(Math.random() * max) ↝ a random integer between 0 (incl.) and max (excl.)
  • Math.floor(Math.random() * (max - min)) + min ↝ a random integer between min (incl.) and max (excl.)
  • Math.floor(Math.random() * ((max + 1) - min)) + min ↝ a random integer between min (incl.) and max+1 (excl.) (OR between min and max both inclusive)
2 of 4
3

Math.random() will give you a "real" number from 0 to 1 (not including 1.0).

That's cool and all, but what if I want a "real" number from 1 to 2?

The answer: "transform" your [0,1) into [1,2).

In practical terms, it means adding 1 to your result.

Try it out -- Math.random()+1 will give you a number from 1 to 2.

In mathematics this is known as a "mapping". That is -- for every possible real number in [0,1), find a way to "map" that real number to another real number in [1,2). That is, if I give you any real number between [0,1), you should be able to map that number -- apply that number to a function that will return a number between [1,2).

In our case, that function f(x) = x+1.

Do you see how this gives us random numbers between [1,2)? Visualize the two intervals next to each other and imagine a line going from every point in [0,1) to its corresponding map in [1,2). Now, pick a random point on [0,1) ... and follow the line. You'll follow the line to a random point in [1,2)!

Now, all complete one-to-one maps from [0,1) to [1,2) will turn a random number between [0,1) to a random number between [1,2)...but not all of them will give you an evenly distributed random number between [1,2). The mathematics behind what maps give you evenly distributed results is a bit complicated but in short, if your map only involves adding, subtracting, multiplying, and dividing by constants, it's "legal" in the sense that the results will also be evenly distributed.

So, now we know how to transform [0,1) into [1,2).

What if I want to map [0,1) onto [0,2)? I can't just add numbers anymore ...

How about I multiply everything by two?

This should work -- the function f(x) = x*2 does indeed map every point on [0,1) to a point on [0,2) --- and because it only involves multiplication by constants (2), it is a distribution-preserving map.

This works! Math.random()*2 will give you a random number between 0 and 2.

Okay, now something a bit more complicated ... transforming [0,1) into [1,3).

Multiplying by two doesn't work ... 0*2 = 0, and that's not in your target range. Adding one doesn't work... even though 0+1 is in your target range and 1+1 is, as well, there is no way you can ever reach 3.

If we can't transform [0,1) into [1,3), let's try and see if we can transform something else into [1,3).

How about [0,2)? Yes, we can do this ... the function f(x) = x+1 perfectly maps [0,2) to [1,3). You can think of + as "shifting" the range up.

And so the solution here is clear -- first, turn [0,1) into [0,2), then turn [0,2) into [1,3).

We already know the first (f(x) = x*2), and we figured out the second (f(x) = x+1). So the "combined" transformation/map is f(x) = (x*2)+1.

That is, Math.random()*2 + 1 will give you a number from 0 to 3.

Now for the final trick...mapping [0,1) to an arbitrary range [min,max).

The secret here is to re-write this as [min,min+range), where range = max-min.

Here you can see that it's simple to transform the range [0,range) to [min,min+range) -- you just add "min" to it. So if I had the range [0,range), and I wanted to get [min,min+range), i would use f(x) = x+min.

So how do we get from [0,1) to [0,range) ?

Multiply by range!

f(x) = (x*range) + min

Now writing things back to original terms, using range = max-min

f(x) = (x*(max-min)) + min

will transform a real number from [0,1) to a real number from [min,max)

I'll leave the rest (turning it into a useful integer) to you

🌐
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?

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