as stated in MDN reference about Math.random()

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.

Since Math.random can return 0, then Math.ceil(Math.random()*10) could also return 0 and that value is out of your [1..10] range.


About your second question, see Most efficient way to create a zero filled JavaScript array?

Answer from Fabrizio Calderan 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 ... max) { const minCeiled = Math.ceil(min); const maxFloored = Math.floor(max); return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); // The maximum is exclusive and the minimum is inclusive }...
🌐
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:
Discussions

javascript - Random Number, Math.floor(...) vs Math.ceil(...) - Stack Overflow
I tried it a few times with an edit of the question's script, not excluding zero, and with one billion random integers generated, which takes roughly 7 seconds to run, and frequencies[0] always returned zero. 2018-08-30T04:40:49.677Z+00:00 ... Wait, if Math.random() returns 16 decimal places ... More on stackoverflow.com
🌐 stackoverflow.com
Using Math.floor(Math.random() ) with an object JavaScript
Chris Roper is having issues with: I trying to write a little practice program to get better at JavaScript. It's a random exercise generator. The idea is it picks an exercise at r... More on teamtreehouse.com
🌐 teamtreehouse.com
1
May 9, 2021
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
Generate A Random Number Within A Range - How Does the Math Work?
Math.floor() is the floor function. Which means it returns the integer part of a number. In your example Math.floor(5.94) = 5 More on reddit.com
🌐 r/learnprogramming
8
0
February 4, 2022
🌐
Codecademy
codecademy.com › forum_questions › 5020be4d3a51800002015ebe
Math.floor and Math.random | Codecademy
Math.random() produces a random number between 0 and 1. This range includes 0, but does not include 1. That means that, through Math.random() alone, you could get a number anywhere between (and including) 0 through 0.999999 (etc). This is a really important fact. 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....
🌐
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
🌐
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…
Find elsewhere
🌐
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.
🌐
Reddit
reddit.com › r/learnprogramming › generate a random number within a range - how does the math work?
r/learnprogramming on Reddit: Generate A Random Number Within A Range - How Does the Math Work?
February 4, 2022 -

Hi all,

I'm learning JS through FCC and it's going okay so far. I'm not a very mathematically inclined person, however, so I struggle with some concepts.

I'm struggling to understand how the following code works;

Math.floor(Math.random() * (max - min + 1)) + min

I tried writing out an example on paper and used 10 and 5 as max and min respectively.

Math.floor(Math.random() * (10 - 5 + 1)) + 5

wouldn't this mean that I can get numbers higher than the max though? Say I get a randomly generated 0.99; well 0.99 * (6) + 5 = 10.94 which is greater than my max of 10?

Am I doing this wrong?

🌐
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 - This function is commonly used ... Math.random() is designed to return a number from 0 up to but not including 1 to ensure a uniform distribution of numbers over the range 0 to less than 1....
🌐
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 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) - how ...
🌐
Codecademy
codecademy.com › forum_questions › 50c386a4a122749bc1006ca6
Math.random and Math.floor explained | Codecademy
December 23, 2012 - 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() ...
Top answer
1 of 3
11

I'll explain this formula:

Math.floor(Math.random() * (myMax - myMin + 1) + myMin);

Say we want a random number from 5-15 (including both 5 and 15 as possible results). Well, we're going to have to work with Math.random(), which only produces values from 0 through approximately 0.99999999999999, so we need to do two tricks to be able to work with this.

The first trick is recognizing that Math.random()'s lowest possible return value is 0, and 0 times anything is 0, so we need to start our range at 0 and adjust to account for this at the end. Instead of calculating 5-15 from the beginning, we recognize that there are 11 values in 5-15 (5, 6, 7, 8, 9, 10, 11, 12, 13, 14, and 15) and count up that many from 0 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) to use 0-10 as our range instead. This is what the myMax - myMin part of the formula does. It defines our new max as 10. Then, at the end of the calculations, we'll just add 5 back to whatever result we get to make the possible result range change from 0-10 to 5-15. This is what the + myMin part of the formula does.

The second trick is recognizing that multiplying Math.random() by our new max range of 10 can only give us a result as high as about 9.999999999999 because Math.random() only goes as high as about 0.99999999999 (never actually 1). When we Math.floor() that later to make it an integer, it brings the result down to 9, so we need to add 1 there to make the maximum possible value 10 instead of 9. That's what the + 1 part of the formula does.


Let's finish this off by walking through an example.

Math.random() can be 0 at lowest and approximately 0.99999999999999 at highest (never 1). Let's just look at what happens with those two cases to see how the range works out.

If we run the case where we've called randomRange(5, 15) and Math.random() gives us 0, here's what we end up with:

Math.floor(Math.random() * (myMax - myMin + 1) + myMin);

=

Math.floor(0 * (15 - 5 + 1) + 5);

=

Math.floor(0 * 11 + 5);

=

Math.floor(0 + 5);

=

Math.floor(5);

=

5

So the lowest value possible is 5. If we run the case where we've called randomRange(5, 15) and Math.random() gives us 0.99999999999999, here's what we end up with:

Math.floor(Math.random() * (myMax - myMin + 1) + myMin);

=

Math.floor(0.99999999999999 * (15 - 5 + 1) + 5);

=

Math.floor(0.99999999999999 * 11 + 5);

=

Math.floor(10.9999999999999 + 5);

=

Math.floor(15.9999999999999);

=

15

So the highest value possible is 15.

2 of 3
3
  1. Math.random() returns a floating point number in the range [0, 0.999999]

  2. (myMax - myMin + 1) returns the integer value

    As an example, if your random range is randomRange(10, 20), then (myMax - myMin + 1) returns the integer value (20-10+1), meaning 11.

    There are 11 possible integers as (10,11,12,13,14,15,16,17,18,19,20)

  3. Math.floor(Math.random() * (myMax - myMin + 1) returns the floating point in the range of (0, myMax-myMin+1)

    (0.99999999999999 * (20 - 10 + 1))=10.99999999999

  4. Therefore we add myMin to get the floating point numbers in the range of (myMin, myMax+1)

    (0.99999999999999 * (20 - 10 + 1)+10)=20.99999999999

  5. Finally, by applying Math.floor() rounds the number DOWN to the nearest integer as 20

🌐
Medium
josephcardillo.medium.com › using-math-random-in-javascript-c49eff920b11
Using Math.random() in JavaScript - Joe Cardillo - Medium
January 31, 2022 - Use .floor to round down to a whole number: console.log(Math.floor(Math.random() * 10)) Use .ceil to round up to a whole number: console.log(Math.ceil(Math.random() * 10)) Use .round to round to the nearest whole number: console.log(Math.ro...
🌐
W3Schools
w3schools.com › jsref › jsref_random.asp
JavaScript Math random() Method
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). Math.random() does not return a cryptographically secure number.
🌐
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?
🌐
CSS-Tricks
css-tricks.com › lots-of-ways-to-use-math-random-in-javascript
Lots of Ways to Use Math.random() in JavaScript | CSS-Tricks
November 30, 2020 - The octave is also randomly selected. Images are stored in an array. A number is generated and multiplied by the number of images in the array via array.length. Then Math.floor rounds the value to a round number and sets the image src in the HTML when the page is loaded or the button is clicked.