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!
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
Confused on second part of the random generator between two numbers.
So I struggled with this one a bit. First I was confused as how to get the Math.random function to generate a random number between 1 and your input, if it only generates 0-1 but not including 1. Then I saw the multiply by 100 + 1, and that made some sense but I did not see how as a newbie, I was supposed to know to start there.
Understanding that, I thought I had the second part done, but then it generated numbers below my lowest number out of the two. What I struggle to grasp here is how I was supposed to know to tell Math.random to take topNumber - bottomNumber + 1, then + bottomNumber. I understand what he’s doing at first, telling it to pick a number between the two to multiple Math.random to, and then add + bottomNumber.
I guess I don’t see how it actually works? If I picked 32 and 64. It would take 32 - 64, which would leave you negative. How is that then supposed to generate a number between 32 and 64? I think I may be overthinking this, and definitely am missing a concept here. Just from the start I had no clue to do anything he is doing here. I feel like I’m lost and wouldn’t have ever known how to get here. Anyone shed some light on this for me?
Steven Parker228,001 Points
The "topNumber" should always be the highest one. You should not have a negative multiplier.
The formulas used to do things like this are more about math than programming. Most programmers don't pull things like this out of their head but look them up in a reference source. Then the programmer's task is to just implement the formula in code.