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
mhammad hamza
Courses Plus Student 636 PointsNeed an explanation!!
Here is my random number solution: var input = prompt("Insert starting number"); var max = parseInt(input); var input1 = prompt("Second number"); var min = parseInt(input1); var randomNumber = Math.floor(Math.random() * (max - min + 1)) + min; var res = randomNumber + " is between " + max + " and " + min; document.write(res);
but i didn't understand this variable var randomNumber = Math.floor(Math.random() * (max - min + 1)) + min; can you explain me how it's work?
5 Answers
Joel Feaster
Courses Plus Student 1,488 PointsMath.random() returns a number from 0 to 1, not including one. Flooring this number will always result in a zero.
If you multiply this float by the max prior to flooring, you will get numbers all the way up to the maximum, including those below the minimum, but not the maximum itself. Say you have 10 as the maximum, you cannot get a 10.
In order to rule out those below the minimum, you subtract the minimum from the maximum. In order to include the maximum, you add one. This is all multiplied by the random floating point number between 0 and 1.
At that point, we aren't quite there. You will be receiving numbers based on the range between the minimum and maximum as explained above. Adding the minimum back in, post-randomizing, ensure that you have numbers that are between the minimum and the maximum.
TLDR: The range of numbers (i.e. the number of numbers between min and max) is defined by those within the floor method's parentheses. The minimum added at the end ensures these numbers are between the desired minimum and maximum.
Joel Feaster
Courses Plus Student 1,488 PointsThis is similar to what we did before for the dice roll, only applying lower bounds to the number.
By subtracting the minimum number from the maximum, you define the range. Say you have minimum of 5 and a maximum of 20, you will get the range of 15. There are 15 possible numbers that you could select at random between these bounds.
This is where you get your random number. Adding the minimum back on, post-randomizing, ensures that you don't need to mess with conditionals. It's much easier as such. Let me know if you need any clarifications!
mhammad hamza
Courses Plus Student 636 PointsThanks Joel. for this good explanation. Really thankful :)
mhammad hamza
Courses Plus Student 636 PointsI didnt understand why i am adding min in the last of this variable: var randomNumber = Math.floor(Math.random() * (max - min + 1)) +min;
Jason Anders
Treehouse Moderator 145,863 PointsHey Mhammad,
Please see a previous answer of mine regarding random number generation in this post.
:)