Is the stated number range of this code wrong?


#1

var damageThisRound = Math.floor(Math.random() * 5 + 1);

The hint for this section describes how the above line of code will generate a number between 1 up to and including 5. I'm I wrong in thinking that 6 is also a possibility in the scenario where Math.random() generates a 1?


#2

No you're not wrong.

Math.random() is a function that produces any number between 0 and 1. if you take this number, multiply it by five, then add 1 you have any number between 1 and 6. Math.floor just makes sure it's a whole number by rounding it down!


#3

I figure it out. I went and looked it up and it seems as if Math.random() includes 0 but isn't inclusive for 1 and therefore it's not possible to generate a 6. I initially though Math.random() could generate a 1.

http://www.w3schools.com/jsref/jsref_random.asp


#4

That would make sense. I guess I was assuming wrong in this! Oh well, it's a good learning experience :smiley:


#5

I asked this exact thing a few days ago and yea it can not generate the number 1.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
Explains more ^.