The numbers from Math.random() are in the range 0 (inclusive) to 1 (exclusive). That means you might get a 0, but you'll never get 1.

Thus that formula starts off with multiplying by an integer, which in your example will give a value from 0 (inclusive) up to 4 (exclusive). If you want random integers like 0, 1, 2, and 3, you have to use .floor() to get rid of any fractional part entirely. In other words, you might get 3.999, and you don't want a 4, so .floor() throws away the .999 and you're left with 3.

Now this presumes that what you want is what most people want, a random integer between a minimum value and a maximum value. Once you understand what Math.random() returns, you're free to generate integers however you want.

Answer from Pointy on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › random
Math.random() - JavaScript | MDN
The value is no lower than min ... (but not equal to) max. ... function getRandomInt(min, max) { const minCeiled = Math.ceil(min); const maxFloored = Math.floor(max); return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); // The maximum is exclusive and ...
🌐
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.
🌐
DEV Community
dev.to › kanaga_vimala_66acce9cf6e › understanding-mathrandom-mathfloor-and-arrays-in-javascript-mkl
Understanding `Math.random()`, `Math.floor()`, and Arrays in JavaScript - DEV Community
June 1, 2025 - Math.random() → gives a number ... "cherry"]; console.log(fruits[0]); // "apple" You can combine arrays with Math.floor() and Math.random() to select random items....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › floor
Math.floor() - JavaScript | MDN
Math.floor(-Infinity); // -Infinity Math.floor(-45.95); // -46 Math.floor(-45.05); // -46 Math.floor(-0); // -0 Math.floor(0); // 0 Math.floor(4); // 4 Math.floor(45.05); // 45 Math.floor(45.95); // 45 Math.floor(Infinity); // Infinity
Top answer
1 of 3
4

Math.floor returns a whole number, while Math.random() will return a float between 0 and 1.

To access an item in an array, like rand[0], you need to have a whole number. You cannot access an item in array with rand[1.43].

That little line of code is going to access a random item in an array by generating a random float from zero to the array's length, and rounding it to its nearest whole number with Math.floor

2 of 3
3

Because there are no fractional array elements

const array = [123, 456];
console.log(array[0.5]);

The code above prints undefined because there is no 0.5 element. There is a 0 element and a 1 element but no 0.5 element

So, to choose a random element with Math.random() which returns a number between 0 and 1 (but not 1) you have to convert to an integer.

JavaScript arrays are not like arrays in most other languages. They're actually more like augmented objects. They act like arrays in that you can get the length (array.length) and call things like push,pop,concat,shift,unshift but you can also add properties

array = [4, 5, 6];
array["foo"] = "bar"
console.log(Object.keys(array));

Prints

[
  "0",
  "1",
  "2",
  "foo"
]

Which explains why the Math.random without the Math.floor would fail. From the semantic point of view of JavaScript an array is just an object with properties named 0, 1, 2, 3 etc. Of course under the hood that's not what JavaScript does as it would be too slow to implement arrays that way but for the most part arrays act like they are actually just an object with properties named '0', '1', '2' and it's perfectly valid to have properties named '0.00012' etc.

Note, it would actually be faster to do this

const rand = array[Math.random() * array.length | 0];

The | 0 is a binary or of 0 which the JavaScript spec effectively says the result is converted to an integer before the | happens. Note that | 0 is not the same as Math.floor. | 0 rounds toward 0 whereas Math.floor rounds down.

         | 0   Math.floor         
------+------+------------
  2.5 |   2  |   2
  1.5 |   1  |   1
  0.5 |   0  |   0
 -0.5 |   0  |  -1
 -1.5 |  =1  |  -2
 -2.5 |  -2  |  -3
🌐
Medium
medium.com › @sanchit0496 › use-of-math-floor-math-random-num-in-javascript-practical-implementation-cc4cb54dafff
Use of Math.floor(Math.random()*num) in JavaScript — Practical Implementation | by Sanchit | Medium
March 26, 2019 - My project uses var randomNumber = Math.floor(Math.random()*(quotes.length)); This outputs a number in the range of 0 and the length of the array. Now we store the randomNumber obtained by this function into document.getElementById(‘quote...
Find elsewhere
🌐
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 › 50c386a4a122749bc1006ca6
Math.random and Math.floor explained | Codecademy
Math.random generates a number ... 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 1 to the answer: Math.floor(Math.random() ...
🌐
Medium
ludmilakorchnoy.medium.com › math-floor-and-math-random-in-javascript-ae63e3bfb91f
Math.floor() and Math.random() in JavaScript | by Ludmila Korchnoy | Medium
January 21, 2021 - I used Math.floor() and Math.random() for my JavaScript project in order for my function to render random images in response to EventTarget.dispatchEvent() method. First, I created an array: const images = [ “image_one.jpg”, “image_two.jpg”, “image_three.jpg”, ]; Then inside of the function I initialize my variable let to the following code, the syntax is not easily remembered but a good one to use: let imageDiv = document.getElementById(“image”); imageDiv.innerHTML = `<img src=”images/${ images[Math.floor(Math.random() * images.length)] }”/>` This is especially handy when there are many elements in the array.
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).

🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › javascript faq
How do `Math.random()` and `Math.floor()` work together? - JavaScript FAQ - Codecademy Forums
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) ...
🌐
CoreUI
coreui.io › answers › how-to-generate-a-random-integer-in-javascript
How to generate a random integer in JavaScript · CoreUI
September 26, 2025 - Create a reusable function: const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min. For array index selection, use Math.floor(Math.random() * array.length).
🌐
Programiz
programiz.com › javascript › library › math › random
JavaScript Math random()
Then it is floored using Math.floor() to make it an integer. Finally, it is added to the smaller number to produce a random number between the given range.