It's great your showing your code and ideas-- good stuff! I think you meant to have a single key/value pair with an array. That makes this work. Good luck with your JavaScript journey! ```javascript const theBigExercise = { exercise: ['Bench Press', 'Shoulder Press', 'Deadlift' , 'Squats'] } function mainExercise() { let randomMain = Math.floor(Math.random() * theBigExercise.exercise.length); console.log(theBigExercise.exercise[randomMain]); return randomMain; } ``` Answer from Jeff Muday on teamtreehouse.com
🌐
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 · In this example, we implement a method called decimalAdjust() that is an enhancement method of Math.floor(), Math.ceil(), and Math.round().
🌐
W3Schools
w3schools.com › js › js_random.asp
JavaScript Random
Math.floor() rounds a number down ... the range is [0, 9]. // Return a random integer from 0 to 10 (both included): Math.floor(Math.random() * 11); Try it Yourself »...
🌐
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() ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › random
Math.random() - JavaScript | MDN
function getRandomInt(max) { return Math.floor(Math.random() * max); } console.log(getRandomInt(3)); // Expected output: 0, 1 or 2 console.log(getRandomInt(1)); // Expected output: 0 console.log(Math.random()); // Expected output: a number from 0 to <1 · js · Math.random() None.
🌐
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 - I’m currently taking the web ... 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) ......
Find elsewhere
🌐
LaunchCode
education.launchcode.org › intro-to-professional-web-dev › appendices › math-method-examples › random-examples.html
Math.random Examples — Introduction to Professional Web Development in JavaScript documentation
floor = 0, ceil = 1, round = 0 floor = 6, ceil = 7, round = 7 floor = 2, ceil = 3, round = 3 floor = 8, ceil = 9, round = 8 floor = 9, ceil = 10, round = 10 · After multiplying Math.random() by 10, applying the floor method gives numbers between 0 and 9. Using the ceil method shifts the range ...
🌐
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 - While making dynamic web projects, ... website. Such a functional code that is very often used in the projects is; Math.floor(Math.random()*num);...
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).

🌐
SitePoint
sitepoint.com › javascript
Math.random and Math.floor - JavaScript - SitePoint Forums | Web Development & Design Community
October 16, 2017 - Hello im new to javascript and i know what Math.floor(Math.random()) * 10; does, But what i don’t understand is why is math floor first and math random second? why dosent Math.random(Math.floor()) * 10; work?
🌐
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 like 0.7382 · * 10 → scales it to 7.382 · Math.floor() → turns it into 7 · An array in JavaScript is a way to store a list of values. let fruits = ["apple", "banana", "cherry"]; console.log(fruits[0]); ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-math-random-method
JavaScript Math random() Method - GeeksforGeeks
July 15, 2024 - For getting a random integer between two values the Math.random() function can be executed in the following way: ... let min = 20; let max = 60; let random = Math.floor(Math.random() * (+max + 1 - +min)) + +min; console.log("Random Number Generated ...
🌐
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 - images[Math.floor(Math.random() * images.length)] }”/>` This is especially handy when there are many elements in the array. This is how random images could be generated in JavaScript project, using Math.floor() and Math.random() function.
🌐
Medium
medium.com › @barrera.jimnlil › so-random-in-javascript-ae9a7e48bd0d
So Random in JavaScript. When your number is up, Math.floor &… | by Liliana Barrera | Medium
August 23, 2022 - What we want is clean and simple. A random, whole number where we set the ranges to whatever we want, right? Enter Math.floor + Math.random.