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

JavaScript JavaScript Basics (Retired) Working With Numbers The Random Challenge Solution

The Random Challenge

Hello. Im learning javascript and I stuck at method with Math numbers. Can someone explain how this line exactly works? Because I dont understand it still..

var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber;

2 Answers

Math.random() generates a random number between 0 and 1 (but not including 1). The smallest number you could get is 0 and the largest almost, but not quite 1 (eg 0.9999999).

If you want to generate a random number between 6 and 9 (say), you want the numbers 6, 7, 8 or 9 - there are four of these. If you calculate topNumber - bottomNumber in this case you get 9 - 6 = 3, but we don't want three numbers, we want four numbers so we need to add 1 to get a multiplier of (9 - 6 + 1) = 4.

Now the smallest random number (0) multiplied by 4 would give 0 and the largest random number (0.99999) multiplied by 4 would give us 3.999999.

Math.floor() gets rid of any numbers after the decimal point so we end up with numbers between 0 and 3 (0, 1, 2 or 3). To get 6 to 9, we need to add 6 (bottomNumber) to all these numbers so (6, 7, 8 or 9).

I figured out finally! But there is other thing I dont understand. Here is code example:

var input1 = prompt("Please type a starting number");
var bottomNumber = parseInt(input1);
var input = prompt("Please type a number");
var topNumber = parseInt(input);
var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber;
var message = "<p>" + randomNumber + " is a number between 1 and " + topNumber + ".</p>";

In website first you need to write inside " Please type a starting number " and then " Please type a number " Lets say I write first 50 and second 35. Then that means javascript (topNumber - bottomNumber) is 35-50 + 1? If I try to make then maths with calculator I cant figure out how can I get random number. If it was 50-35 opposite then I understand everything. I dont get how code still making random numbers if its (topNumber fx "35" - "50" bottomNumber). Blowing my mind.. Lol

You need to make sure that the second number is bigger than the first. You would normally have these two inputs in a loop and check the values before allowing the program to move on to the next stage.

If the second number is less than the first, it will still produce some random numbers but, in your example with 35 and 50, they would be between 35 and 21 (35 -14). The -14 is what you get when you do (35 - 50 +1).