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 Overflowjavascript - Random Number, Math.floor(...) vs Math.ceil(...) - Stack Overflow
Using Math.floor(Math.random() ) with an object JavaScript
Basic JavaScript - Generate Random Whole Numbers with JavaScript
Generate A Random Number Within A Range - How Does the Math Work?
Videos
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?
Math.floor() is preferred here because of the range of Math.random().
For instance, Math.random() * 10 gives a range of [0, 10). Using Math.floor() you will never get to the value of 10, whereas Math.ceil() may give 0.
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?
why don't we use Math.ceil() instead of using the floor() function then adding 1, wouldn't it be much easier?
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.
Math.random()returns a floating point number in the range [0, 0.999999](myMax - myMin + 1)returns the integer valueAs 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)
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
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
Finally, by applying
Math.floor()rounds the number DOWN to the nearest integer as 20