Math.floor(Math.random() * 5 + 1);


#1

Math.floor(Math.random() * 5 + 1);

First we use Math.random() to create a random number from 0 up to 1.
For example, 0.5
Then we multiply by 5 to make the random number from 0 up to 5.
For example, 0.5 * 5 = 2.5
Next we use Math.floor() to round down to a whole number.
For example, Math.floor( 2.5 ) = 2

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

what is the objective of this last one?


#2

Math.random will generate a random number 0 and 1 (one and zero not included), if you multiply this by 5, you can get the values: 0, 1, 2, 3 and 4, make sense? Then you add one, so now the possible outcomes are 1, 2, 3, 4 and 5


#3

math.random generate a random number between 0 and 1?


#4

Yes, any random number between 0 and 1.

I have one more question:
The description in the hint says:
First we use Math.random() to create a random number from 0 up to 1. For example, 0.5
Then we multiply by 5 to make the random number from 0 up to 5. For example, 0.5 * 5 = 2.5
Next we use Math.floor() to round down to a whole number. For example, Math.floor( 2.5 ) = 2
Finally we add 1 to change the range from between 0 and 4 to between 1 and 5 (up to and including 5)

Why is "Math.floor" executed before the "+1"? The whole expression "Math.random()*5 + 1" seems to be in brackets? So shouldn't it be "(random number x 5+1)" with the outcome rounded?


#5

Guess i was not paying attention, math.floor gets executed after you multiplied by 5 and added one


#6

@stetim94 close but 0 is included only 1 is not :wink:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

@stef2k16 The question is: "Does it make a difference"? And the answer is :"No"

Let's do the math. You have your random number times 5 and you have 1. Let's make it shorter and say
x = Math.random()*5.

Now you could do 3 orders:

Math.floor(x+1)
Math.floor(x)+Math.floor(1)
Math.floor(x)+1

and all yield the same result because 1 floored is still 1 so the important part is just Math.floor(x) and whether you first add one or do it later is up to your preference.


#7

If it generates a number between 0 and 5 and that its multiplied by 5 it goes up to 4? But .99 * 5 = 4.95? Which is more then 4.


#8

The Math.floor() function returns the largest integer less than or equal to a given number. the largest integer in 4.95 is 4. So it returns 4


#9

You are right, it doesn't make a difference in this case. But if I get the wrong understanding of priorities it could make a difference at some point in the future. :slightly_smiling:

Thanks for all your answers!


#10

This is something I don't understand. Since it takes 4 hits to slay the dragon, why should we time 5? Shouldn't we time 4, then add 1, so that the range will be 1, 2, 3, 4?


#11

but if you do times 5, the range will be 0,1,2,3,4. Then we can also have no damage. Lets say the random number is 0.000001. Times 5 that makes 0.000005. If you floor this, you will get zero. The highest number (again, not sure how many numbers after the decimal are generated, doesn't matter):
0.999999999999 * 5 = 4.999999999995

if we floor this, we will get 4. Floor doesn't round, it throws everything away. And the random number generated is lower then 1, so you will never get 5.


#12

Why do we need to get 5 since it takes 4 hits to kill the dragon?


#13

Did you even bother to read my answer? It explains this, and your reply seem to have no connection to my response


#14

Of course I read your answer, and I really appreciate your reply. I should've written down my thinking process. There must be somewhere I got wrong, so I still don't understand. Suppose Math.random() generates 0.9. If Math.random() * 4, then it will be 3.6, then 3.6 + 1 = 4.6, Math.floor() makes it 4. If Math.random() generates 0.1, then Math.random() * 4 will be 0.4. 0.4 + 1 = 1.4, Math.floor() makes it 1. So the range will be 1, 2, 3, 4. Since it takes 4 hits, why should Math.random() times 5? Also, since +1 is within Math.floor(), how to get zero? Thanks a lot for your great help.


#15

Now both 4 and 5 will kill the dragon. If only 4 will get the dragon, changes of you winning are pretty slim. Seems you can't deal 0 damage after all.


#16

I see, so to make it 5 simply increases the chance to win. So the range is 1, 2, 3, 4, 5, not 0, 1, 2, 3, 4, 5. How to get zero anyway in case you failed to hit once? Really appreciate your help.


#17

Sorry I think I got it. youHit already makes it either 0 or 1. So the chance of hitting zero is already included. Thank you very very much for your help. Have a great day!


#18

I can't believe your answer is "The question is: "Does it make a difference"? And the answer is :"No""!!

I am trying to learn Javascript and you guys just taught me the wrong operator precedence!!! i spent hours reading and re-reading reference material to find out why it was computed in the order you said it was, and the answer is you were completely wrong.

PLEASE correct this ASAP as you are teaching people something totally wrong. Look here:

https://www.mathsisfun.com/operation-order-bodmas.html

http://www.w3schools.com/js/js_arithmetic.asp
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

The Math.floor() is computed LAST.

THANKS for the great course by the way... apart from this horror :slight_smile:


#19

First of all this post is factually "dead" for two month so please do not revive the zombies unless it is necessary and if you do provide some more context as rereading the 17 posts of an answered question isn't that much fun, thank you.

As far as I recall it the question was:

Or in code why:

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

and not

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

And my answer was, illustrated by examples, that in the end they yield the same result because flooring an integer value isn't doing that much. This had nothing to do with operator precedence whatsoever but with a basic understanding of math. And quite frankly I don't see where this should be wrong, so could you elaborate on your point?

And what do your links have to do with the problem?
If you take the last of them which is probably the most useful one:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
You can see that javaScript intuitively applies PEMDAS (minus the E because an exponential operator is not implemented yet) and as said how is this contradicting what I stated? And what should the other two links tell me? How PEMDAS works? And even if this was the case, the important question there would be how does javaScript handle it and not how math does. Because for example the "=" operator is quite different from it's use in math.

PS: In the end it comes down to solving this task:
Generate random integer numbers in a certain interval while having:
- basic math +-*/
- a function generating random floating point numbers from 0 to 1 (excluded actual 1)
- a function that rounds down to the next integer

go ahead and mix these to fulfill the task of generating a random integer number in the range from x to y. And I'd assume that it is less about a "best way" to do this but more about getting to know basic functions by applying them to a simple problem.


#20

sorry to revive an old post, i was not aware of any forum etiquette that prohibited this, but it is my first post so i apologize.

i will try to simplify my point, it is purely relating to the code hint being wrong, and you not acknowledging this.

I understand the result in this case is the same, but i am concerned with bad information in the code hint. I demonstrated why the code hint is wrong with the links that show the PEMDAS rule both in simple maths, and also in the context of Javascript itself.

  • in the code hints for this part of the course is clearly states the order that the expression:

Math.floor(Math.random() * 5 + 1);

executes is in THIS ORDER:

  1. Math.random()
    02 *5
    03 Math.floor()
    04 +1

Are you saying this is the correct order, or will you agree with me that this is in fact WRONG and the order should be:

  1. Math.random()
    02 *5
    03 +1
    04 Math.floor()

Thanks, I am not trying to annoy you, i would like to just get this straight and hopefully get the course edited so people don't waste the time I did trying to find out why code academy said one thing and all other sources said something else.