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 trialTam Nguyen
5,795 PointsNumber Range Confusion
In the second part of the challenge, the number range from 10 to 25 is used as an example. The point is to generate a random number between two numbers, 10 and 25. The explanation of how to generate the random number starts around 03:30 in the video. When you add the bottom number, in this case 10, Dave says "…a random number from 10…0 + 10, to 25…15 + 10." (This occurs around 03:50 of the video.) How is the bottom number a random number from 10 if we've already declared the bottom number to be 10?
Maybe the verbiage is a little confusing. I understand that in order to get a random number between 10 and 25, you must subtract 10 from 25 to get a range or the difference then multiply that by the Math.random() method. Adding the bottom number at the end, which in this case is the number 10, keeps the random number within the range of 10 and 25. Is this correct? I more or less understand the logic, but am having difficulty in following the explanation. Any clarification would be much appreciated. Thanks.
4 Answers
Antonio Fran
3,965 Points<h2>Explanation of following code on range [10,25]</h2>
<pre>var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber;</pre>
////////////////
<ul> <li>Math.random() - gives a decimal number from 0 up to 1, but not including 1</li>
<li>(topNumber - bottomNumber + 1) - for (25 - 10 + 1) it gives 16</li>
<li>if you multiply 16 by a "decimal number that is greater or equal 0, but lower than 1", you will get a "decimal number that is greater or equal 0, but lower than 16"</li>
<li>if you floor that number, it will never be able to be 16, but be able to be integer form 0 to 15</li>
<li>now, if you add 10 to an integer from 0 to 15, you will get an integer from 10 to 25</li>
<li>all we need to do now is store the result in a variable we have just declared</li> </ul>
Tam Nguyen
5,795 PointsThanks, Antonio. I think I understand the logic. We are adding the bottomNumber 10, to the generated random number. Adding the bottom number keeps in within the range of 10 and 25(25 being the topNumber). Is that correct?
What is confusing is when he says "…in other words a random number from 10, 0 plus 10, to 25, 15 plus 10." Where does he get "0 + 10" and "15 + 10" from?
He says it around 03:47 in the video.
Antonio Fran
3,965 PointsThat's correct.
Where does he get "0 + 10" and "15 + 10" from?
Well, remember when I have said: "... if you add 10 to an integer from 0 to 15 ..."?
If you apply that to only extremes, not the whole range, you will get: "you add 10 to 0" and "you add 10 to 15".
Antonio Fran
3,965 PointsSo, it is not calculation, it is just his explanation of adding a number to a range and getting a different range.
You add a number to old range's extremes and you get a new range's extremes.
Danielle Howard
4,402 PointsOk I've finally understood it. This is my explanation for anyone else who is having difficulty understanding this equation:
Basically bottomNumber is being added to 0, and bottomNumber is also being added to the result of topNumber minus bottomNumber. So this means that the random number is being generated from between bottomNumber and topNumber.
m mm
Courses Plus Student 539 PointsWhat's wrong with this?
var randomNumber = Math.floor(Math.random() * topNumber) + bottomNumber;
Is it valid too?
Antonio Fran
3,965 PointsFor bottomNumber = 20 and topNumber = 50, "Math.floor(Math.random() * topNumber)" will give a random number from a range from 0 to 49 (random() never returns 1).
Therefore, once you add 20 to that random number, you will get a random number from a range from 20 to 69 and I assume you wanted to get a random number from a range from 20 to 50, so it is not valid.
On the other hand, this is also one valid example for getting range from bottomNumber to topNumber
var randomNumber = bottomNumber + Math.round( Math.random() * (topNumber-bottomNumber ) )
"Math.round()" can either return grater or lower integer from, in this case 0 to 30, so if you add that to 20, you will get a range from 20 to 50.
Danielle Howard
4,402 PointsHi, I'm really confused by this equation. I can't work out which part says generate a random number 'between' the two numbers.
The way I am reading it it says generate a random number between 0 and the top number.
Please help
Tam Nguyen
5,795 PointsTam Nguyen
5,795 PointsThis is the line of code where I felt the explanation was difficult to comprehend:
var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber;
At around 03:47 of the video, Dave says, "…in other words a random number from 10, 0 plus 10, to 25, 15 plus 10. Don't worry if you don't understand the math…"
If 10 is used as the bottom number and 25 is used as the top number, I don't understand what he means when he says "0 +10" and "15 + 10."
link to video