Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialja5on
10,338 Pointsrandom number formula help
Math.floor(Math.random() * (6 - 1 + 1)) + 1;
gets changed to
Math.floor(Math.random() * (highNum - lowNum + 1)) + lowNum;
I cannot ever memorise where highnum or lowNum go, it just doesn't compute for me, but lets say i blindly just do it
therefore if highNum = 20 and lowNum = 5
Math.floor(Math.random() * (20 - 5 + 1)) + 5;
I get that 20 - 5 gives the range that random number can choose and +1 stops random selecting 0, but why add the 5 again later?
4 Answers
Steven Parker
231,248 PointsThe + 1
in the range calculation isn't related to the low number limit, and a value of 0 will always be one possibility after multiplying the range. But the final + 5
shifts the range up into the desired values, and guarantees that the lowest possible value returned will be 5.
ja5on
10,338 PointsI'm not sure what you mean in your last message. If I use the first +1 I can see by running the program that 0 is never returned thats why in my previous message I said "I thought the + 1 is to stop getting 0, seems to be what Guil is saying before."
Still unclear as to how the second +1 works.
Steven Parker
231,248 PointsSee my additional comment.
ja5on
10,338 PointsMath.floor(Math.random() * (6 )) // output is 0 - 5. 6 numbers returned but JavaScript counts 0 first.
Math.floor(Math.random() * (6 - 1)) // output is 0 - 4. gives 5 numbers.
Math.floor(Math.random() * (6 - 1 + 1)) // output is 0 - 5. here returns 6
Math.floor(Math.random() * (6 - 1 + 1)) + 1; // output is 1 - 6. This moves the range up by 1. 0 never returned.
by the way Math.floor(Math.random() * (6 + 1 - 1)) + 1 also works.
or a simpler Math.floor(Math.random() * (7 - 1)) + 1 works as well.
Steven Parker
231,248 PointsOr just Math.floor(Math.random() * 6) + 1
(range of 6 values, minimum value 1).
You only need the full formula when the minimum and maximum come from variables and are not known in advance.
ja5on
10,338 Pointshahaaha, I can't outsmart you :-) Thanks.
Steven Parker
231,248 PointsIt was never a contest, we are all here to learn. Happy to help.
ja5on
10,338 Pointsja5on
10,338 PointsI thought the + 1 is to stop getting 0, seems to be what Guil is saying before, is this right? The final + 5 means the lowest possible outcome is 5?
Hope I'm thinking correctly now, thanks for your concise explanation, it has really helped.
Steven Parker
231,248 PointsSteven Parker
231,248 PointsThe original formula for simulating dice rolling had
+ 1
in two places. The first one is inside the range calculation, and it makes the range inclusive of the limits. The second one, added after the range is multiplied by the random value, is what sets the minimum value.Steven Parker
231,248 PointsSteven Parker
231,248 PointsTo illustrate what I was saying:
Consider what happens if you leave either one off