The wording of the hint in Dragon Slayer part 2 is confusing to me. Help?


#1

Hello,

I'm confused about the following hints:

Then we multiply by 5 to make the random number from 0 up to 5. For example, 0.5 * 5 = 2.5

Finally we add 1 to change the range from between 0 and 4 to between 1 and 5 (up to and including 5)

If we multiply by 5 to make the random number from between 0 to 5, then wouldn't adding one make the random number between 1 and 6?


#2

Here is how Math.random works. It will provide value between 0 and 1, but with only 0 included (it's [0,1) if you understand it).

So lets say you pick 0.99. When it's multiplied by 5 it will provide value that is close to 5, but not 5. When you add 1 you will be close to 6, but not at 6. And then Math.floor() kicks in and rounds the number to closest downward integer.

More examples.

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

Lets say we roll 0.4 at first roll. It will result in 0.8 floored, and give 0, meaning we didn't hit the dragon. Anything over 0.5 rolled on random will results in value over 1 when multiplied, and exactly at 1 when floored, meaning we hit the dragon.
Same goes for damage. We roll 0.1 and multiply it by 5, so we get 0.5. Then we add 1 so we have 1.5 and when we floor it, we get 1. We did 1 damage.
If we roll 0.95 and multiply it by 5, we get 4.75. Then we add 1 and we're at 5.75! But, we still have to floor it so we get 5 and that is the damage we do the dragon.

That's why damage range is 1 to 5, because of the floor and because of Math.random() never being able to randomly pick 1.


#3

Perfectly worded . I understand now