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 trialkelley riley
3,159 PointsWhy is the correct solution correct? It is different from just the die roll from earlier.
During the die roll example, we had: (Math.floor(Math.random() * 6) + 1);
But for our solution it's: document.write(Math.floor(Math.random() * secondNum - firstNum) + firstNum);
Why do we have to subtract the min value from the max value? Why wasn't that necessary in the die roll app?
1 Answer
Steven Parker
231,268 PointsThe die roll is a special case of the more general formula.
Also, you didn't get that formula quite right, its actually this:
Math.floor( Math.random() * (secondNum - firstNum + 1) ) + firstNum
Where firstNum is the lowest number of the range, and secondNum is the highest. This formula works for any range. But if we are simulating a die roll, we know that that firstNum is 1 and secondNum is 6. So we can plug those into the formula and get this:
Math.floor( Math.random() * (6 - 1 + 1) ) + 1
Now since (6 - 1 + 1)
is the same as 6
, we can simplify it even further to just this:
Math.floor(Math.random() * 6) + 1
...which is exactly what you used for the die roll example.
kelley riley
3,159 Pointskelley riley
3,159 PointsThanks for you correction. I think that's the main issue I was having with this code.