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 JavaScript Basics (Retired) Working With Numbers Create a random number

instead of 6, can we use a multiplier of 7 and get the same results?

i was curious if we could do

Math.floor(Math.random() * 7);

to achieve the exact same results. why make things sloppier with adding a 1 at the end?

3 Answers

Steven Parker
Steven Parker
231,275 Points

The random range normally starts at 0, the 1 is added to shift the range up to start at 1.

Unless you want to simulate 7-sided dice that can roll 0-6. :wink:

OOOOOOOHHHHHHHHH!!!!!! got it!

thanks for a fast reply. makes total sense now

Ben Reynolds
Ben Reynolds
35,170 Points

That "+1" he added at the end doesn't make it 6 + 1. It's outside the parentheses, so it's the result of Math.floor(Math.random() * 6) plus one. If it wasn't done that way, no matter what you used as a multiplier could still end up rolling a 0.

Thanks for taking the time to respond. I fully understand it now.