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 trialBaljit Padda
6,979 PointsStruggling to understand how to process a user's lowest number when generating a random number in their specified range.
Hello Treehouse,
I am struggling to understand how you write the code that generates the random number between the range we ask of the user. i.e. if they give the lowest number as 10 and the highest number as 25. Please can you explain this more.
I managed to generate a random number when it was just using a highest number/Max value but could someone please explain how you do this with two user-given values. i.e. how do you know to subtract highest value - lowest value and + 1 etc. ???
Thanks for your help!
Baljit
1 Answer
Steven Parker
231,248 PointsStarting about 1 minute into the video, the instructor shows and explains changing the code to handle both upper and lower limits.
The highest value - lowest value + 1 part of the formula is how you determine how many possible random values there will be, so you can multiply that by the random function. Then, since the range of random numbers begins at 0, you add the lowest value to shift the range into the desired position.
Baljit Padda
6,979 PointsBaljit Padda
6,979 PointsHi Steven,
Thank you for explaining this further. Thanks for your help! :)