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 trialAndrew Lundy
13,269 PointsThe math doesn't make sense to me.
After reading the formula, on Treehouse, MDN and FreeCodeCamp - this math doesn't make much sense.
Math.random() returns a number from 0 to 1 right? It then multiples that number by 'high minus low plus one' and then adds the low to it?
1 Answer
Balazs Peak
46,160 PointsI'd suggest to run experiments to gain more insight. I created a workspace for you on rextester: http://rextester.com/MHSTG13787
- You will find out that adding +1 makes the upper limit inclusive. If you take that away, the upper limit will never be returned (it will be an exclusive upper limit)
- Also, you'll be able to see that the operations with Math.Random only creates a range. If you take away the addition of the lower limit at the end, you'll see that the numbers will range from 0 to (upper-lower), and not from lower to upper.
It is a really good habit to quickly experiment with code any time you don't understand something! Keep coding ;)
Andrew Lundy
13,269 PointsAndrew Lundy
13,269 PointsI ended up figuring it out. Lol it was what I was thinking. I just had to process the math behind the code! Thanks man.
Balazs Peak
46,160 PointsBalazs Peak
46,160 PointsYou are most welcome!