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 trialArikaturika Tumojenko
8,897 PointsIs it essential to understand this exercise in order to progress?
I can't understand the second part of the random number challenge, no matter how many answers I read. Can I move on with this track if I can't get this into my head - is it a very important brick of the learning progress that will affect the understanding of other concepts?
4 Answers
Arikaturika Tumojenko
8,897 PointsI can't understand the syntax. Although I understand exactly what Math.floor and Math.random do, I don't get why do we add one (+1) to the subtraction of the first number from the second. Is this something you learn in Math classes (I'm not the best when it comes to equations so maybe that's why I can't wrap my head around it?). It's very frustrating because seeing the answers I realize I could never find the solution on my own. I feel like something is missing from the lessons.
Simon Coates
28,694 Pointsfunction getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
When working with math functions, it's often best to start small. so let's say we want numbers in a range of 2 to 4. The value of (4 -2 + 1) is 3. You multiply 3 by a random value in the range of 0 to 0.9999999999999. Once you apply floor, you are left with either 0, 1, 2. Add the lower number to these and your possible values are 2, 3, 4.
This level of maths is pretty common. A lot of array handling, string work and some database stuff requires differences and displacement. but you can usually break the maths down, use sample values and experiment until you come up with the formula that works. But a lot of maths in computing is pretty simple once you've done it a couple hundred times.
Arikaturika Tumojenko
8,897 PointsThank you for you answer but this doesn't make it any better for me :). Why +1? What is the value of that 1? We are already talking about functions and arrays, something I didn't get to it yet. I hope sooner or later all the things your are trying to explain will get a bit clearer to me.
Simon Coates
28,694 PointsThe random number is in the range of 0 to 0.999999999999. If you want a value in 0-4 (ie. range is 4), then the smallest number you could get is 0*4 or 0. The biggest number is 0.999999999 * 4, which is 3.9999999999999 - which floors to 3. It's not big enough. So you increase the range by one. 5 time 0 is 0. the largest value is 5 * 0.9999999, which is 4.999999999. This floors to 4.
Arikaturika Tumojenko
8,897 PointsI understand the idea for the first part of the exercise where the 1 is outside the parenthesis. But in the second part it's inside the max-min subtraction parenthesis, so I don't understand why. Shouldn't the formula be (Math.random() * (max-min)) + 1? Anyway, I'll just move on, I don't seem to get what's going on in this exercise but it's ok. Thank you for your time.
Simon Coates
28,694 PointsIt's the same equation. In the first part, he wanted variation from 1 to the top limit. minimum was implicitly always 1.
Math.floor(Math.random() * (max - min + 1)) + min; //formula
Math.floor(Math.random() * (max - 1 + 1)) + 1; //apply minimum of 1.
Math.floor(Math.random() * max) + 1;
Ivana Lescesen
19,442 PointsThank you so much. You are amazing :)
Imran Chowdhury
5,684 PointsThank you Simon! You're explanation is brilliant. You cleared up the same confusion I had of Arikaturika Tumojenko.
Simon Coates
28,694 PointsSimon Coates
28,694 Pointscan you provide details about what you don't understand?