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 trialAsparuh Tenev
5,599 PointsHow can we get random number of 10 in the example given in the video ?
The example in the video uses the numbers 25 and 10. And so, if (25-10) is 15 , then Math.floor will generate number between 0 and 14. If we add +1 and make it (25-10+1) then Math.floor will generate number between 1 and 15. Same as 1 to 6 with the example of the die. In this case of 1 to 15, we add the bottom number of 10 to receive a value between 16 and 25. So the possible values are including the max value of 25 but makes it impossible to get the minimum value of 10. Am i missing something? Because in the video Dave said we can get value from 0 to 15 from the (bottomNumber-topNumber +1) and i don't see how.
danielperez
Full Stack JavaScript Techdegree Student 15,993 PointsAsparuh Tenev,
In answer to your question, using the example number range and code from the video,
first the the range used in the example is between 10 and 25.
second, the process of getting to final random number is at least 5 steps
I made a snapshot of my workspace : https://w.trhou.se/imfj766va8
see line 5 of random.js
var randomNumber = Math.floor( Math.random() * (topNum - btmNum + 1) ) + btmNum;
1: get the number range
evaluating this line of code starts with (topNum - btmNum + 1)
this is to get the number range
given the topNum = 25,
and the btmNum = 10
25 - 10 = 15
so that the random number will be equal to or greater than bottom number, add 1
15 + 1 = 16
so range = 16
2: Math.random()
Math.random() generates a floating point number between 0 and 1,
so I ran Math.random() in a java console about 20 times:
here are 2 floating numbers generated from Math.random()
and 1 unlikely but realistic floating point number
finally got a lower floating point number : 0.04556772115922936
an unlikely but realistic floating number : 0.06249999999998
a higher real random num : 0.9641306385668025
3: multiply by the range of 16
multiply these random numbers by the range of 16,
with lower random number
0.04556772115922936 * 16 = 0.72908353854767
from an unlikely but realistic floating number
0.06249999999998 * 16 = 0.99999999999968
from an real higher random number
0.9641306385668025 * 16 = 15.42609021706884
4: math.floor()
from Mozilla Developer Network,
"function returns the largest integer less than or equal to a given number."
so it rounds down to the nearest whole integer
from lower random number
Math.floor(0.72908353854767) would return: 0
from an unlikely but realistic floating number
Math.floor(0.99999999999968) would return 0
from higher random number
Math.floor(15.42609021706884) would return: 15
5: make sure the random value falls between the range of 25 and 10
so add the bottom number
so from a rounded down number of 0
0+10 = 10
from a rounded down number of 15
15+10 = 25
Summary: So using the code in the video or in https://w.trhou.se/imfj766va8...
a lower initial floating point number of 0.04556772115922936
would result in 10
an initial floating point number as high as 0.06249999999998
would still result in 10
where as a higher initial value like 0.9641306385668025 or even 0.9999999999999999,
would result in 25
Hope this helps
Daniel Perez
2 Answers
Steven Parker
231,248 PointsWhen the "bottomNumber" is 10, and the "topNumber" is 25, then topNumber - bottomNumber + 1
will be 16, so multiplying that by Math.random()
will produce a random number between 0 and 15 (inclusive).
Then adding "bottomNumber" (10) to that produces a final number between 10 and 25.
Note the the formula for the range value is topNumber - bottomNumber + 1
(not "bottomNumber-topNumber +1").
Asparuh Tenev
5,599 PointsYup, got that. Thanks for the quick answer Steven! The answer to how we get 0 is that if we get a random number, 0.0001 lets say and multiply it by 16 then we get 0.0016. add 10 and will be 10.0016, then Math.floor will make it 10, and this is how a random number of 10 is produced. Thanks again, great community!
Steven Parker
231,248 PointsSteven Parker
231,248 PointsCan you provide a link to the video?