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

KaWai Guo
KaWai Guo
2,788 Points

This guy is terrible at explaining...

@3:15. what does he mean by adding the bottom number to the results???

Nico Julian
Nico Julian
23,657 Points

Yo,

So bottomNumber is equal to whatever the user provided as input, let's say we choose 2. Likewise, topNumber is equal to what they gave for the upper limit, let's say 11.

When you go to generate a random number, Math.random() gives you a value between 0 and 1, something like 0.35896; let's act as if it were a percentage and call it 36%.

When the dude does (topNumber - bottomNumber + 1), he's getting the span between highest and lowest, in our example that would be 11 - 2 + 1, which is 10. That is, [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]. Now we take 36% of that 10, and get 3.6. He then uses Math.floor on it, getting 3. Finally, he adds 1, resulting in 4.

This process will always result in a random value within the range of possible values.

KaWai Guo
KaWai Guo
2,788 Points

but even when i took out the (+ 1) after (topNumber - bottomNumber) it is still able to generate within the range of the input. In my head, the "+ 1" seems pointless...

Thanks for the reply.

3 Answers

var input = prompt("Please type a number 1 : "); var input2 = prompt("Please type a number 2 : ");

var topNumber = parseInt(input); var topNumber2 = parseInt(input2);

var randomNumber2 = Math.floor(Math.random() * topNumber) * Math.floor(Math.random() * topNumber2) + 1 var message2 = "<p> " + randomNumber2 + " is a number between " + topNumber + " and " + topNumber2 + " </p>" document.write(message2);

Roger Hwang
Roger Hwang
3,851 Points

You're probably rounding numbers differently and you'll have to test all edge cases to make sure it's generating the right random numbers between the two.

LMAO, he's not terrible at explaining, he probably missed a loophole or didn't think of other solutions.

Checkout my code which does the exact same thing except it's way shorter and easier:

var no1=prompt("Type a Number bitch"); var no2=Math.ceil(Math.random()*no1); alert("Random number until "+no1+" is "+no2);

You're right, the +1 is pretty pointless lol. He's rounding down and adding 1, I'm rounding up and not adding anything. Same pinch. Tested out 1 to 2 20 times and never gave me an incorrect output. So... Yeah.