8.2 Explaining Code


#1

Why does the + 1 in the equation change the range from between 0 and 4 to between 1 and 5? And what is the significance of the empty parentheses in the Math.random() . I may be overthinking things, but I am struggling with learning the basics and wanted to make sure I am not missing an important concept.


#2

Math.random() is a function, and much like other functions it runs off of parameters. Essentially it's between 0 and 1 (not to include 1)

0 <= Math.random() < 1.

So if you take that number, and modify that afterwards that's how you can get more than just the 0-1 answers.

So you can do

Math.random() * 5 + 1, because mathematically speaking "Math.random() * 5" means any number between 0, and up to but not including 5, are numbers that are allowed. Adding 1 after the multiplication means any number between 1, and up to but not including 6, are numbers that are allowed.

After determining this number we use "Math.floor()" to round down the decimals to the nearest whole number:

Math.floor(5.9) would be 5.0
Math.floor(3.99999999999) would be 3.0
Math.floor(2.1) would be 2.0 and so on.


Let me know if you need me to go into any more depth with this subject, but I until then I hope this helps!