Math.random() provides a random number from [0,1) (floating point: '[' = inclusive, ')' = exclusive).

So in Math.floor( (Math.random() * 10) + 1); multiplying Math.random() by 10 will provide a random number from [0, 10).

The +1 after the multiplication will change the output to be [1, 11).

Then finally Math.floor( ... ) converts the random number that is in the range from [1, 11) to an integer value.

So the range of the executed statement will be all integers from [1, 10]. Or to be more specific it will be one of the numbers in this set: { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

Answer from Patrick Barr on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › random
Math.random() - JavaScript | MDN
function getRandomIntInclusive(min, max) { const minCeiled = Math.ceil(min); const maxFloored = Math.floor(max); return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled); // The maximum is inclusive and the minimum is inclusive } ... This page was last modified on Jul 10, 2025 by MDN contributors.
🌐
W3Schools
w3schools.com › js › js_random.asp
JavaScript Random
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:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › floor
Math.floor() - JavaScript | MDN
* @param {number} exp The exponent (the 10 logarithm of the adjustment base). * @returns {number} The adjusted value. */ function decimalAdjust(type, value, exp) { type = String(type); if (!["round", "floor", "ceil"].includes(type)) { throw new TypeError( "The type of decimal adjustment must be one of 'round', 'floor', or 'ceil'.", ); } exp = Number(exp); value = Number(value); if (exp % 1 !== 0 || Number.isNaN(value)) { return NaN; } else if (exp === 0) { return Math[type](value); } const [magnitude, exponent = 0] = value.toString().split("e"); const adjustedValue = Math[type](`${magnitude}e$
🌐
Codecademy
codecademy.com › forum_questions › 50c386a4a122749bc1006ca6
Math.random and Math.floor explained | Codecademy
Math.random generates a number ... To get it to be a whole number, i.e. an integer, apply Math.floor, which rounds down to the nearest whole number: Math.floor(Math.random() * 10) To get a whole number between 1 and 10, add ...
🌐
Codecademy
codecademy.com › forum_questions › 5020be4d3a51800002015ebe
Math.floor and Math.random | Codecademy
It means that Math.random() * 10 can never actually get you a 10. So, when you use Math.floor(Math.random() * 10), it gets you an integer (a whole number) between (and including) 0 to 9.
🌐
Codecademy
codecademy.com › forum_questions › 4f8ac89ba5f7b5000300cc17
Math.floor(Math.random()*10)+1 generates 10, not 0 | Codecademy
In section 6>Random Stuff>Math.floor I used: Math.floor(Math.random()*10)+1 to generate score and got a checkmark and “That’s correct” message.
Find elsewhere
🌐
Quora
quora.com › How-does-Math-floor-Math-random-*-10-+-1-would-generate-a-random-number-between-1-and-10
How does Math.floor ((Math.random() * 10) + 1); would generate a random number between 1 and 10? - Quora
Answer (1 of 3): Math.random() will give a number in the range between 0.01 to 0.99 then you are multiplying that number with 10. That is the output range now is between .1 to 9.9 then you add 1 so range is between 1.1 and 10.9 and then over ...
Top answer
1 of 6
25
Hi Chelsea, Maybe it will help to see how the formula can be derived starting only with Math.random() and Math.floor(). That way you'll know that it's correct and more importantly why it's correct. I'll try to provide a conceptual understanding of what's going on with the formula. We know that Math.random() returns a value in the range [0, 1). 0 is included but 1 is excluded. You can think of it as the range 0 to 0.999999... Now let's pass that result to the Math.floor() function. js Math.floor(Math.random()); Since floor will truncate the decimal and give you back the whole number part we're always going to get 0 here. Make sure you understand that or the rest isn't going to make any sense. Ask questions if you need to. We can only get 1 integer out of this. It's not so much important that the integer is 0 but that we can only get 1 integer out. If we are going to be able to get more numbers out of this then we need to make that range bigger. When you multiply something by 2 it becomes twice as big as it was before. It scales up by a factor of 2. Let's see what happens when we multiply Math.random() by 2 js Math.floor(Math.random() * 2); That will give us a new range of 0 to 1.999999... which is twice as big as the range we started with. What happens when those numbers are passed into Math.floor? All the numbers generated from 0 to 0.9999... will be truncated to 0 and all the numbers from 1 to 1.9999... will be truncated to 1 Now we're able to get 2 different integers out of this. If we multiply by 2 we can get 2 numbers back. It stands to reason that if we multiply by 6 then we will be able to get 6 numbers out. That will give us a range that's 6 times as big, 0 to 5.99999..... I won't write it all out but after passing through the floor function you would get js 0 to .99999... -> 0 1 to 1.99999... -> 1 ... 5 to 5.99999... -> 5 In general, whatever you multiply Math.random() by is how many integers you'll be able to generate. Now we can start deriving the formula and I'll use a specific example to help. Let's say we want to generate numbers from 5 to 10 inclusive. We need to know how many numbers are there. Setting up the variables - js var max = 10; var min = 5; If we list them out, 5, 6, 7, 8, 9, 10 and count them we see that there are 6 total numbers. We know from before that we're going to have to multiply by 6 in order to get 6 numbers out. How can we come up with the 6 using our max and min variables?? If I do max - min I get 5 which is 1 short. max - min gives you the distance from 5 to 10. You always have to add 1 to that if you want the total amount of numbers. That gives us the expression max - min + 1 Putting that into the formula, js Math.floor(Math.random() * (max - min + 1)); It's important that max - min + 1 is enclosed in parentheses so that all of that happens before the multiplication. At this point the formula can generate the correct amount of numbers but they always start at 0 because the range from Math.random starts from 0. 0, 1, 2, 3, 4, 5 // What we have 5, 6, 7, 8, 9, 10 // What we want Notice that if we add 5 to all the numbers in the first row, we'll get the second row. 5 is what our min value is in the example. So if we add the min value onto the end of our formula, it will shift all the numbers over to the ones we want. js Math.floor(Math.random() * (max - min + 1)) + min; You can think of it as a 2 step operation, you scale up the range, and then you shift it. I meant for this to be shorter but I hope it helps you out.
2 of 6
0
So glad I found this! Excellent explanation.
Top answer
1 of 7
19

Math.random returns a floating-point number between 0 and 1.

Returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.

Multiplying this by n gives a floating point number between 0 (inclusive) and n (exclusive).

Math.floor is then used to convert this floating point number to an integer between 0 and n - 1 (inclusive).

2 of 7
17

Why would anybody call Math.floor on a Math.random result?

In a nutshell, one calls Math.floor() when you want to truncate a decimal value to its nearest integer (by just dropping the decimal portion. So, 3.9 becomes 3, 2.1 becomes 2, etc... So, you would typically use that when you need an integer and you want the integer that is smaller than or equal to the decimal value. The math library also has Math.ceil() and Math.round(). Math.ceil() gets you the next larger integer, Math.round() rounds to the nearest integer going either up or down depending upon which is closer.

I've seen it used like: Math.floor(Math.random() * num);

Breaking Math.floor(Math.Random() * num) down into it's individual pieces and explaining each piece, you get this:

Math.random() gives you a random decimal number between 0 and 1, including 0, but not including 1. So, it might give you something like 0.38548569372.

Math.random() * num gives you a random decimal number between 0 and num, including 0, but not including num. So, if num was 10, it might give you 3.8548569372.

Math.floor(Math.random() * num)) gives you a random integer number between 0 and num, including 0, but not including num. So, it might give you 3.

Math.floor() truncates the decimal number to only the integer portion. A random integer is often used for getting a random value from an array (which needs to be an integer).

🌐
freeCodeCamp
freecodecamp.org › news › javascript-math-random-method-explained
JavaScript Math.random() Method Explained
January 25, 2020 - For example, if you need to select randomly from an array of 10 elements, you would need a random number between 0 and 9 inclusive (remember that arrays are zero indexed). var x = Math.floor(Math.random()*10); console.log(x); // 7
🌐
W3Schools
w3schools.com › jsref › jsref_random.asp
JavaScript Math random() Method
A random whole number between 1 and 10 (inclusive): let x = Math.floor((Math.random() * 10) + 1); Try it Yourself » · A random whole number between 1 and 100 (inclusive): let x = Math.floor((Math.random() * 100) + 1); Try it Yourself » · The Math.random() method returns a random floating point number between 0 (inclusive) and 1 (exclusive)....
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › javascript faq
How do `Math.random()` and `Math.floor()` work together?
April 5, 2019 - Hello! I’m currently taking the web development path and I’m stuck on the third part, Javascript-Built in objects. I don´t understand the Math.floor part, can anyone help me? console.log(Math.random()*100); ( this is the first part) Math.floor(Math.random)() *: ( this is the second part) ...
🌐
Codecademy
codecademy.com › forum_questions › 5198adbdbbeddf9726000700
How to get a random number between X(any number) and X(another number)? | Codecademy
Math.floor(Math.random() * ((y-x)+1) + x); Example 1: a whole number between 2 and 10 would be: // x = 2, y = 10 Math.floor(Math.random() * ((10-2)+1) + 2); Math.floor(Math.random() * 9 + 2); Example 2: a whole number between 5 and 10 would ...