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 trialSteven Mejia
2,098 Pointsinstead 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
231,269 PointsThe 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.
Steven Mejia
2,098 PointsOOOOOOOHHHHHHHHH!!!!!! got it!
thanks for a fast reply. makes total sense now
Ben Reynolds
35,170 PointsThat "+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.
Steven Mejia
2,098 PointsThanks for taking the time to respond. I fully understand it now.