With regards to the range

Tell us what’s happening:
Describe your issue in detail here.

The problem I have is…why would we want to increase our range when the question asks for numbers equal to min and max, if we’re adding 1 to the formula we’re increasing our range, so end of day we’ll have values that’s greater than max instead of being equal, or am I missing something here?
I have put my max and min in brackets (still works with +1 outside of those brackets) because the +1 pertains to whole range instead of just min

  **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
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36

Challenge: Generate Random Whole Numbers within a Range

Link to the challenge:

Floor rounds down. What happens when you remove the +1 and try it a bunch of times? What numbers do and don’t you get?

1 Like

We have to add 1 because we want the range to be inclusive, meaning that it should be possible for the function to return myMax.

Math.random() generates a number between 0 and 1, but will never result in 1. So we need to increase add 1 so that it is possible to get numbers between myMax and myMax + 1. The Math.floor() function will then round all those numbers down to an integer.

2 Likes

thank you, but wouldnt that number then increase the range? meaning that it’ll actually be greater than max number?
So if the range were 5-10 (bumped up to 5 thanks to min :wink: ) then with adding 1 and max were “10” it’ll now be 5-11 right or am I thinking this wrongly?

As far as I understand the question we’re supposed to make our range = to min and max, not increase the range?

oh…I think I get it…
we’re adding the 1 because the decimal doesn’t quite make the max number so we’re adding the 1 to equal the max number :wink:

Am I right?
But if this is the case…why then doesn’t the function then add 1 to the min then as well instead of just max?

It looks like you’ve got it.

Let’s walk through a simple version.

Math.random() will generate a number between 0 and 1. It will never give you the number 1 though, because it’s not inclusive. If we do Math.floor(Math.random()) the only possible result is 0. If we want to get a number between 0 and 2 (without getting 2), then we can do so with Math.random() * 2. This will multiply the number between 0 and 1 (non-inclusive) by 2, giving us a decimal number between 0 and 2 (non-inclusive). Then when we do Math.floor(Math.random() * 2), there are two possible values: 0 and 1.

Even though we might think of the “range” as only being one number (because 1 - 0 is 1), we actually want 2 possible results.

The random() function makes a decimal between 0.0 and 1.0, but it never makes a value of 1.0.

You take this value and multiply it by the number of integers you want (if you want numbers from 11 to 15, that would be 5 = 15-11+1), giving a number between 0.0 and 5.0 but excluding 5.0.

You then round down, getting 0, 1, 2, 3, or 4.

You then shift this number up to the range you want. By adding 11 you get 11, 12, 13, 14, or 15.

Because of the rounding down(or up if you prefer) our range needs to include an ‘extra’ integer.

oh, so if you’re adding 1, you’re not actually affecting the min value (min value will always be at 0 unless…you’re adding “min” to the formula)?

but wouldn’t the adding of 1 actually increase min as well? it would make sense though if it did, or does min stay at 0 no matter what value you add to the range (only if you add min to the formula would you increase min), this is one thing that I’m a little stuck on

thank for your help

Math.random() always gives you a number between 0 and 1. We’re just multiplying that result by myMax - myMin + 1, so the lowest number stays 0.

so using Math.random the values will always be 0 to whatever? if we want a value higher than 0 we’d need to use the min value? (no other value can be used to increase min except min itself right?)

Yes. The random() function always makes a number between 0.0 and 1.0

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.