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 Functions Arrow Functions Function Challenge Solution

random formula

Math.floor(Math.random() * (6 - 1 + 1)) + 1;

gets changed to

Math.floor(Math.random() * (highNum - lowNum + 1)) + lower;

I cannot ever memorise where highnum or lownum go, it just doesnt compute for me, i
spent weeks trying to get it.
What do i do now?

Victor Stanciu
seal-mask
.a{fill-rule:evenodd;}techdegree
Victor Stanciu
Full Stack JavaScript Techdegree Student 11,196 Points

This is how I managed to understand and learn the formula. Think about mathematical intervals https://en.wikipedia.org/wiki/Interval_(mathematics). First, you need to remember a few things:

  1. Math.random() generates a number in the half-open interval [0,1) (means number X can take a value from 0 up to 0.9999.... , but never 1).
  2. Math.floor() takes the random number, say 10.4567.. and takes the first integer lower that that number (in this case, 10).
  3. Mathematical operations applied to intervals go like this: [a , b) - c = [a - c , b - c). Simple!

First and most simple example is getting a random number between lets say 1 and 10. There a three steps to achieve that.

  1. You must multiply the interval [0,1) by 10, getting a new half-open interval [0,10) meaning X cand get a value from 0 up to, 9.9999..., but never 10.
  2. You add 1 to the interval, and get [1,11), meaning X can get a value from 1 up to 10.99... but never 11.
  3. This is where Math.floor() comes in handy (see the example above).

Take note that the addition of 1 is the last mathematical step to getting the desired half-open interval!!

Now, lets say you want a random number between 3 and 7. For this, you obviously can't just use the exact same steps from above. I find the easiest way to be the reverse-engineering of the desired interval, meaning: We want the interval [3,7) and need to do some basic math operations to get to the interval [0,1). Keep in mind the way Math.floor() works!! This means we are actually working with the interval [3,8) (just like in the above example, were we got interval [1,11)).

  1. From [3,8) substract the lower limit of the interval, meaning 3, like this: [3,8) - 3 = [0,5).
  2. Divide the new interval [0,5) to the upper limit, meaning 5, to get the Math.random() interval: [0,5) / 5 = [0,1).

I hope this helps! Good luck in your coding adventures!

3 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

The best way to memorise the problem is to keep attempting. We all learn best in programming by doing things. This way you'll be more empowered to tacklem new problems as they come. And at the end of the day that's what coding is all about.

Anyway let me try and get your head around this for you. I confused myself in my previous answer to you.

function getRandonNumber(lower, upper = 100) {

    return Math.floor(Math.random() * (upper - lower + 1)) + lower;
}

This is one example solution... the one as defined in the solution video. :)

It uses a function definition as an example rather than an arrow function and takes 2 parameters. One is the lowest range of random numbers and the other is the highest, with a default value.

Inside it returns the formula. It defines Math.floor() which itself contains a range between the upper lower and the lower number. We're saying +1 in the formular becuse we want to ensure we get to the highest range of the random number if it is generated.... by default, the methods don't do it. And then we add the value of lower to get the calculated lower value range.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

It seems like you're getting some practice in with functions, which is why you're changing the integers into variable names like highNum and lowNum.

function randomNumber(highNum, lowNum = 100) => {

    return Math.floor(Math.random() * (highNum - lowNum + 1)) + nowNum;

}

Then pass the numbers, (the integers) into your function call.

function randomNumber(highNum, lowNum = 100) => { return Math.floor(Math.random() * (highNum - lowNum + 1)) + nowNum; } So if I call:- randomNumber(10, 50); it means 50 - 10 gives a difference of 40 40 + ?

I think this question is all wrong

I want to know how I can memorize/understand the formula