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 trial

JavaScript

random 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
Steven Parker
231,271 Points

The + 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.

I 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
Steven Parker
231,271 Points

The original formula for simulating dice rolling had + 1in 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
Steven Parker
231,271 Points

To illustrate what I was saying:

Math.floor(Math.random() * (6 - 1 + 1)) + 1;
//                                ^^^
//                                this one makes the range inclusive,
//                                the range still includes 0
Math.floor(Math.random() * (6 - 1 + 1)) + 1;
//                                      ^^^
//                                      this one sets the minimum
//                                      (and why 0 is never returned)

Consider what happens if you leave either one off

                                          // by leaving off the first one
Math.floor(Math.random() * (6 - 1)) + 1;  // you'll get 1-5 instead of 1-6
                                          // by leaving off the second one
Math.floor(Math.random() * (6 - 1 + 1));  // you'll get 0-5 instead of 1-6

I'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
Steven Parker
231,271 Points

See my additional comment.

Math.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
Steven Parker
231,271 Points

Or 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.

hahaaha, I can't outsmart you :-) Thanks.

Steven Parker
Steven Parker
231,271 Points

It was never a contest, we are all here to learn. Happy to help.