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

I don't understand why the lower number is subtracted from the higher number, then the lower number is added.

Hi,

I don't really understand the explanation behind subtracting the lower number from the higher one, then adding the lower one again. Is there a simpler explanation to why this is done? Thanks!

2 Answers

Steven Parker
Steven Parker
231,141 Points

The lower is subtracted from the higher and then one is added to produce the range. This value represents how many possible numbers the random choice will pick from. But this range of numbers will always begin with 0.

The lower number is added after multiplying (picking from) the range to shift the range back where the minimum and maximum should be.

Oh, ok! I get it now. Thanks so much!