I don't get the range concept - Math.random() * (max - min + 1)) + min

I’ve RSAd a few things around the net including this one and this one.

I’m nearly there but need one more push over the mind hurdle.

I understand how:

Math.random() * (max - min + 1)

… creates the distance between the min and max.

What makes no sense to me is how adding min at the end of :

Math.floor(Math.random() * (max - min + 1)) + min

… lets the computer know that it needs to shift that distance down or increase the min value.

My thinking goes:

Math.random( ) * (6 - 2 + 1)
Math.random( ) * 5
= 1.5 …(for example)
Math.floor(1.5) + 2
1 + 2

Where has my understanding of this gone wrong?

12 Likes

You’re right. We multiply by the size of the range to scale the random number. We then add the minimum value to “shift” our scaled random number into the range.

6 Likes

I hate to bump older posts but I flat out just do not understand this at all. I do have ADHD so it’s hard to follow, so pls be kind :slight_smile:

17 Likes

No worries!

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

But we don’t want a random decimal; we want a random number. We can get that by multiplying our Math.random() result by the size of the range. For example, if we want a random number between 0 and 10, we need to multiply by 10. A result of 0.4 would become 4. If we want random numbers from 7 to 11, that is 5 possible numbers (7, 8, 9, 10, 11).
This is where we get Math.random() * (max - min + 1)

We don’t want decimal numbers though, we want whole numbers. Math.floor() just chops off the decimal. 3.14159 becomes 3.
That’s where we get Math.floor(Math.random() * (max - min + 1)).

Now we’re getting a number from 0 to whatever our range size was. If we wanted a number from 7 to 11, it’s a random number from 0 to 5. To make that be a number from 7 to 11, we just need to add 7 to whatever number we got. 7 is our minimum value.
This leads us to Math.floor(Math.random() * (max - min + 1)) + min

182 Likes

You’re a total hero. Exactly what I needed and thank you so much!

10 Likes

I know it’s been a while, but here I am & I thought I’d throw in my 2 cents just in case it helps someone else. Combined with ArielLeslie’s description above it helped me.

I don’t think verbally, so this is how I had to work it out:

Math.floor(Math.random() * (max - min + 1)) + min;

// max = 15; min = 5
Math.floor(Math.random() * (15 - 5 + 1)) + 5;
Math.floor(0 * (15 - 5 + 1)) + 5;
Math.floor(0) + 5;
0 + 5;
5

// max = 15; min = 5
Math.floor(Math.random() * (15 - 5 + 1)) + 5;
Math.floor(0.9999999999 * (15 - 5 + 1)) + 5;
Math.floor(0.9999999999 * (11)) + 5;
Math.floor(10.9999999989) + 5;
10 + 5;
15
55 Likes

Perfect explanation, I was struggling until I read this.

2 Likes

I’m glad it helped. Happy coding!

3 Likes

hey, you seem to understand it better than me.
Should below code not produce the very same result as the one suggested in the solution?

function randomRange(myMin, myMax) {

var result = Math.round(Math.random()* (myMax - myMin)) + myMin;

return result;
}

you need to +1 to (myMax - myMin).

What we’re trying to achieve with the above is to gather the range of numbers between myMax and myMin, to be later multiplied by a decimal between 0 and 1(non-inclusive).

Imagine this;
if myMax = 10, myMin = 5

per your formula,
10 - 5 = 5

However,
the numbers between 5 and 10 inclusive are as follows:
5, 6, 7, 8 ,9 ,10
there are a total of 6 numbers.

This is why we add 1 to (myMax - myMin).

9 Likes

I do not understand what the “+1” on (max-min+1) is for…

I see. They’re comparable to arrays with zero indexing?

1 Like

Correct me if I’m wrong but the 1+ seems to just be the part where it counts up the array.

3 Likes

There is no array involved here.
The +1 is needed if we have an inclusive range. Remember that Math.random() is non-inclusive of 1. If our min is 2 and our max is 5, we want to include 2, 3, 4, 5. That’s four numbers. But 5-2 is 3. That’s why we add 1.

31 Likes

Sorry once again this is a little old, but I just wanted to add to this (maybe others can benefit from different wording):

This was how I would explain why add 1 (similar to the explanation by @ArielLeslie) :

When the range is first found, this value is an exclusive range (doesn’t include the min and max ). However, due to the way Math.random() works, this becomes a half-inclusive range, where it includes the min but not the max (refer to previous exercise). Thus, to make this an inclusive range, you add 1 to it to compensate for the missing spot of the max .

15 Likes

This is a good answer,

2 Likes

hope this helps someone, I did the math for both the minimum and maximum cases to understand the formula better

let’s say Math.random() = 0.99 (maximum case)
example input: min = 5, max = 10

formula with the 1 added :
Math.floor(Math.random() * (max - min + 1)) + min
Math.floor(Math.random() * (10 - 5 + 1)) + 5
Math.floor(Math.random() * (6 )) + 5
Math.floor(0.99 * (6 )) + 5
Math.floor(5.94) + 5
5 + 5
10

formula without the 1 added :
Math.floor(Math.random() * (max - min )) + min
Math.floor(Math.random() * (10 - 5 )) + 5
Math.floor(Math.random() * (5 )) + 5
let’s say Math.random() = 0.99
Math.floor(0.99 * (5 )) + 5
Math.floor(4.95) + 5
4 + 5
9

10, the maxium input, will not be inclusive/never be reached in the formula without the 1


now let’s say Math.random() = 0.00 (minimum case)

formula with the 1 added :
Math.floor(Math.random() * (max - min + 1)) + min
Math.floor(Math.random() * (10 - 5 + 1)) + 5
Math.floor(Math.random() * (6 )) + 5
Math.floor(0.00* (6 )) + 5
Math.floor(0) + 5
0 + 5
5

formula without the 1 added :
Math.floor(Math.random() * (max - min )) + min
example input: min = 5, max = 10
Math.floor(Math.random() * (10 - 5 )) + 5
Math.floor(Math.random() * (5 )) + 5
Math.floor(0.00 * (5 )) + 5
Math.floor(0) + 5
0 + 5
5


TL;DR
without the 1 added to the formula, the minimum number within the range will be included/reached, but never the maximum number
minimum was 5, maximum was 10
via https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-within-a-range
we want to be inclusive of the minimum and the maximum number

13 Likes

Just replying to say I’m doing this in 2020 and did not understand this at all until I saw your explanation. Your explanation is still helping people out, 2.5 years later!! How awesome is that? Thank you! :smile:

2 Likes

I’m really glad that I could help! Thanks for letting me know.

Happy coding!

2 Likes

This was an awesome explanation, thanks!

1 Like