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
Larry Young
4,356 PointsWould someone please check my understanding of Math.random?
var input1 = prompt("Please provide a low number!"); var bottomNumber = parseInt(input1); var input2 = prompt("Plase provide a high number!"); var topNumber = parseInt(input2); var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber; document.write(randomNumber);
//For example: for a random number between 10 and 50.... 50(topNumber) - 10(bottomNumber) + 1= 41.... Math.random gives us a number between 0 and 41...then add bottomNumber (10)..51..than math.floor takes it back down to 50 because it does not include 51 so 50.9999 will be rounded down to 50.
this is my little weird explanation to myself on how the math is working on this. Can someone look over this to make sure i understand what the guy has said? I think i do but the way he explains the math is a bit confusing to me. Probably seems silly but thank you!
1 Answer
Steven Parker
243,656 PointsYour explanation reflects a correct understanding of how the formula works, along with the Math functions involved, from the standpoint of the maximum possible value.
Just to confirm, when you say "between 0 and 41" the number picked could be 0 but it could approach but never reach 41.
Larry Young
4,356 PointsLarry Young
4,356 PointsThanks for the assurance Steven!