Creating a random whole number within a range

Tell us what’s happening:
Hello all.

I have a question regarding creating a random whole number within a range. I can mentally follow what is happening up until (max - min + 1)) + min;

My question is, I understand how this line of code creates a “ceiling”(Max) for the number that will be created, but how does this create a “floor”(minimum)? I am guessing it has something to do with the '+ min" at the end, but my brain is not understanding how this creates a “minimum” value.

For instance, Math.floor(Math.random() * (max - min + 1))
^ I understand this portion. It’s showing us that our range is between 80 and 20, and we are adding one because the challenge requires it to be inclusive of the max value using math.floor. But 80 - 20 = 60. How and where does this become a range between 80 and 20? Doesn’t make sense. Thanks for your time.

Your code so far

function randomRange(myMin, myMax) {
// Only change code below this line
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
// Only change code above this line
console.log(randomRange(40, 20));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0.

Challenge: Generate Random Whole Numbers within a Range

Link to the challenge:

For simpler math problems like these I often find it helpful to run a few examples through the function using real numbers.

So let’s use randomRange(10, 20) and let’s say Math.random() returns 0, then you have:

(0 * (20-10+1))

which is 0 and of course the floor of 0 is 0. But then the last thing you do is add the min, so now you get 10. So you can see that even if random returns 0 you will still return your min value because you are adding it at the end.

1 Like

Math.random returns a number between 0 and 1. Don’t try to guess how it works, put numbers into the equation. What happens if you put the same number in but leave off the final number?

Note floor and ceiling are just maths rounding functions, the bit you’ve posted doesn’t create a floor or ceiling (floor rounds down to nearest integer, ceiling rounds up), it’s confusing to use those terms when one of those functions is being used here anyway

In addition to the answers above, the answers to this stackoverflow question might answer your question.

1 Like

Ok got it. I Ho ISDF INSDI LO.

I understand.

But if (0 * (20 - 10 + 1)) = 0 and then we add 10 (min) we just get 10, and 0 is stuck at 0 because this is all within a math.floor function?

I’m not sure I understand your question. You never want the answer to be zero because we called the function as randomRange(10, 20) so the minimum value it should return is 10 and that’s why we add myMin after the Math.floor() is done.

Think of it this way, if we called the function as randomRange(0, 10) then in the function body we could basically get rid of of the + myMin at the end because it would be 0 and the function would still return a number between 0 and 10. So the Math.floor(Math.random() * (max - min + 1)) part by itself functions exactly the way we need it when the min passed in is 0.

Now instead of 0 through 10 let’s say we want 5 through 15. It’s the exact same thing as needing 0 through 10, but we just need to shift the answer over 5 spaces to get 5 through 15.

0   ...   10
     5   ...   15

5 through 15 is still finding a random number between a range of 11 consecutive numbers, just like 0 through 10. So we can think of 5 through 15 as 0 through 10, and then once we get the answer we just need to shift it 5 to the right by adding 5, the min value.

I think it clicked. I’m starting to see it in my head. I see why we add the minimum at the end, it’s because even in the scenario that we DO get 0 from the math.random() input, we are setting the minimum range at whatever we add at the end. And the range depends on what we allow it to be within the equation.