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
Danny L.
10,837 PointsQuestion about the math from 0 not lowNumber?
I have a question about the math involved.
Here's a bit of the transcript that I was able to follow:
"For example, if I want a random number from 10 to 25.1:41 Well, 25 minus 10 plus 1 is 16.1:45 So this would generate a random number from 0 up to but not including 16.1:50 By providing that value to Math.floor, I'll get a random number from 0 to 15.1:56"
I understand it until this point.
Here's where I got lost:
"Then, I add the lowest number which produces the number from 10 to 25.2:02 This, I don't understand, why does adding the lowest number cause the range to go from 0 - highNumber to lowNumber - highNumber?"
For the sake of clarity I'll use actual numbers instead of variables below:
If this code:
Math.floor(Math.random() * (25 - 10 + 1)
generates a random number between 0 and 16 but not including 16, essentially 0-15 then why does adding the 10 in the code below:
(Math.random() * (25 - 10 + 1) + 10);
turn the range from 0-16 to 10-25 and not 0-25?
I was able to get it to work but I really want to understand the math.
Here is my final code for this:
let lowNumber = parseInt(prompt("Enter a low number:"));
let highNumber = parseInt(prompt("Enter a high number:"));
while (isNaN(lowNumber)) {
console.log("That's not a number. Please enter a number.");
lowNumber = parseInt(prompt("Enter a number:"));
}
while (isNaN(highNumber)) {
console.log("That's not a number. Please enter a number.");
highNumber = parseInt(prompt("Enter a higher number:"));
}
let randomNumber = Math.floor(Math.random() * (highNumber - lowNumber + 1) + lowNumber);
console.log(`${randomNumber} is a number between ${lowNumber} and ${highNumber}`);
1 Answer
Adam N
70,280 PointsThe first block of code, as you said, generates a number from 0 to 15. But we always add 10 to this random number. So, some examples:
First block generates 0, then 10 is added to it, making the final result 10
First block generates 5, then 10 is added to it, making the final result 15
First block generates 15, then 10 is added to it, making the final result 25
^ That should illustrate how adding 10 makes the final result land in the specific range that we're looking for.
Let me know if this helps!
Danny L.
10,837 PointsDanny L.
10,837 PointsHey thanks for answering, I seem to understand how to get the maximum number in a range, like you said, adding 10 makes the final result up to 10, etc.
What I don't understand is the minimum number in a range, why does this code result in a random number from 10-25 and not 0-24. Where in the math does it specify that the minimum number in this case would be 10?
(Math.random() * (25 - 10 + 1) + 10);