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 Numbers The Math Object Random Number Challenge – Two Numbers Solution

Pimwipha Cheaptumrong
Pimwipha Cheaptumrong
10,977 Points

(JavaScript Number) Random number challenge - Two Number Solution.

I wonder why we can't just use:

const randomNumber = Math.floor( Math.random() * highNumber ) + 1;

why it has to be :

const randomNumber = Math.floor( Math.random() * (highestNumber - lowestNumber + 1 ) + lowestNumber;

Btw I am not very good at Math. Please help. Thank you!

4 Answers

Cornelius Hager
Cornelius Hager
5,659 Points

Without using lowestNumber the number that is generated will always be between 1 and your highNumber.

Example

If you use these numbers for example:

lowestNumber = 10 highestNumber = 15

The code below will give you a random number between 1 and 15. This is not what you want. As the randomNumber could also be a 2 or 4. This number is lower than your provided lowestNumber of 10.

const randomNumber = Math.floor( Math.random() * highNumber ) + 1;

So you need to somehow tell your code from what lowest number it should start generating the random number. If you look at your range 10 - 15. You realise, I actually only need a range of 5 numbers: from 1 - 5, as the numbers from 1 - 10 are unnecessary for the calculation of the number between 10 and 15.

So, how do you calculate this cleverly?

As we just established you actually need the size of the range. 1 - 5 in the case above. How do you get that range? Simply by tacking the highestNumber and subtracting the lowestNumber. 15 - 10 = 5. Nice. That's the range we need.

Let's examine the correct code.

const randomNumber = Math.floor( Math.random() * (highestNumber - lowestNumber + 1 ) + lowestNumber;

The code runs, from the inner most parentheses to the outside.

1.

highestNumber - lowestNumber + 1

Here the range which is actually needed is calculated. See my example 15 - 10 = 5. The + 1 is added so the range starts from 1 and not from 0.

2.

Math.random() * (highestNumber - lowestNumber + 1 )

The range is multiplied with the random number generated by Math.random. (Remember the generated number is between 0-1 (e.i. 0,121213 or 0,99123123)

3.

Math.floor( Math.random() * (highestNumber - lowestNumber + 1 ))

The generated number for your range is being rounded down to a flat integer.

4.

randomNumber = Math.floor( Math.random() * (highestNumber - lowestNumber + 1 ) + lowestNumber;

Now the final magic is happening. Let's get back to our 10 - 15 example. We subtracted 10 from 15. We got 5. We got a random number between 1 and 5. Let's say 3. Now you only need to add back the 10 to the 3 and tada!! you have 13. A random number between 10 and 15.

Took me a while as well to wrap my head around it.

Chris Goyette
Chris Goyette
2,910 Points

But what if we don't generate a 3 but a 0 instead?

Then the random number will be 10.

If we display the code in console.log() it would say something like "10 is a random number between 10 and 15."

Now that doesn't make much sense to me.

Cornelius Hager
Cornelius Hager
5,659 Points

Good point Chris, I didn't explicitly mention this in my comment as this is thoroughly explained in the videos before this exercise.

In my code that's what the +1 is for. As the Math.random() never returns a flat 1 the result can be 11, 12,13,14. Check the documentation here for more info: https://www.w3schools.com/js/js_random.asp

Wow, this was very helpful. Thank you for breaking it down in chunks that can be understood well! Now I get it!!!! Thank you.

Cornelius - this was a great explanation. Thanks!

Skyler Harris
Skyler Harris
7,670 Points

Awesome. Great breakdown, this is what helped me finally understand this random number challenge solution.

Math.random() gives a number between 0 and 1.

By giving a high number and a low number (and creatively multiplying) you can create a number in any range.

Chris Goyette
Chris Goyette
2,910 Points

Thanks Cornelius. That link you sent me to w3schools was very helpful.

Angela Davis
seal-mask
.a{fill-rule:evenodd;}techdegree
Angela Davis
Full Stack JavaScript Techdegree Student 3,110 Points

Very helpful explanation thank you. I had run my code as shown below and it was working for me, but your version and explanation works so much better and make logical sense.

let randomNum = Math.floor(Math.random() * highNum) +lowNum;