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

**URL:** <https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [January 24, 2018, 11:35am UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630 "2018-01-24T11:35:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ijemz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ijemz/32/72663_2.png) [@ijemz](https://forum.freecodecamp.org/u/ijemz)\
**Post date:** [January 24, 2018, 11:35am UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/1 "2018-01-24T11:35:37Z")

</div>

I’ve RSAd a few things around the net including [this one](https://forum.freecodecamp.org/t/help-lost-generate-random-whole-numbers-within-a-range/71372/7) and [this one](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187).

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?

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [January 24, 2018, 1:17pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/2 "2018-01-24T13:17:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Emmanade](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/emmanade/32/81415_2.png) [@Emmanade](https://forum.freecodecamp.org/u/Emmanade)\
**Post date:** [March 16, 2018, 8:27pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/3 "2018-03-16T20:27:39Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [March 16, 2018, 8:51pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/4 "2018-03-16T20:51:50Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![Emmanade](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/emmanade/32/81415_2.png) [@Emmanade](https://forum.freecodecamp.org/u/Emmanade)\
**Post date:** [March 16, 2018, 8:54pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/5 "2018-03-16T20:54:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![skillitzimberg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/skillitzimberg/32/134735_2.png) [@skillitzimberg](https://forum.freecodecamp.org/u/skillitzimberg)\
**Post date:** [April 3, 2020, 10:48pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/6 "2020-04-03T22:48:20Z")

</div>

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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![andrewj720](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andrewj720/32/123693_2.png) [@andrewj720](https://forum.freecodecamp.org/u/andrewj720)\
**Post date:** [April 13, 2020, 12:10pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/7 "2020-04-13T12:10:30Z")

</div>

Perfect explanation, I was struggling until I read this.

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [April 13, 2020, 2:47pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/8 "2020-04-13T14:47:54Z")

</div>

I’m glad it helped. Happy coding!

---

<div class="post-metadata">

**Author:** ![lukaszkukla](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lukaszkukla/32/137664_2.png) [@lukaszkukla](https://forum.freecodecamp.org/u/lukaszkukla)\
**Post date:** [April 28, 2020, 10:35am UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/9 "2020-04-28T10:35:18Z")

</div>

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;  
}

---

<div class="post-metadata">

**Author:** ![eltontayzy](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@eltontayzy](https://forum.freecodecamp.org/u/eltontayzy)\
**Post date:** [May 21, 2020, 10:28am UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/10 "2020-05-21T10:28:14Z")

</div>

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)`.

---

<div class="post-metadata">

**Author:** ![olibadua](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/olibadua/32/146438_2.png) [@olibadua](https://forum.freecodecamp.org/u/olibadua)\
**Post date:** [May 25, 2020, 1:54pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/11 "2020-05-25T13:54:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![olibadua](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/olibadua/32/146438_2.png) [@olibadua](https://forum.freecodecamp.org/u/olibadua)\
**Post date:** [May 25, 2020, 1:55pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/12 "2020-05-25T13:55:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KittyKora](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kittykora/32/124629_2.png) [@KittyKora](https://forum.freecodecamp.org/u/KittyKora)\
**Post date:** [May 25, 2020, 2:10pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/13 "2020-05-25T14:10:59Z")

</div>

> [@ArielLeslie](#):
>
> No worries!
> 
> `Math.random()` gives us a random number between `0` and `1` .  
> `0` but never quite return a `1`
> 
> But we don’t want a random decimal; we want a random _number_ .  
> 0.1 X  
> 1 V  
> We can get that by multiplying our `Math.random()` result by the size of the range.  
> 0 1 2 3 4 5 6 7 8 9  
> whole number between `0` and `9` .  
> so thats our 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` .  
> 10x 0.4 = 4  
> If we want random numbers from `7` to `11` , that is 5 possible numbers ( `7` , `8` , `9` , `10` , `11` ).  
> there are 5 numbers  
> This is where we get `Math.random() * (max - min + 1)`  
> the 1 + because  
> 7 + 1 = 8 + 1 = 9 + 10 + 1 = 11
> 
> 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  
> 7 8 9 10 11  
> 1 2 3 4 5  
> 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.  
> 7\< is the starting number  
> This leads us to `Math.floor(Math.random() * (max - min + 1)) + min`

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

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [May 25, 2020, 6:01pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/14 "2020-05-25T18:01:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bryan123bg](https://avatars.discourse-cdn.com/v4/letter/b/3be4f8/32.png) [@bryan123bg](https://forum.freecodecamp.org/u/bryan123bg)\
**Post date:** [July 18, 2020, 11:15am UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/15 "2020-07-18T11:15:11Z")

</div>

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` .

---

<div class="post-metadata">

**Author:** ![geraldombuthia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/geraldombuthia/32/132306_2.png) [@geraldombuthia](https://forum.freecodecamp.org/u/geraldombuthia)\
**Post date:** [July 18, 2020, 12:21pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/16 "2020-07-18T12:21:33Z")

</div>

This is a good answer,

---

<div class="post-metadata">

**Author:** ![yayimadinosaur](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yayimadinosaur/32/88251_2.png) [@yayimadinosaur](https://forum.freecodecamp.org/u/yayimadinosaur)\
**Post date:** [September 11, 2020, 1:44am UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/17 "2020-09-11T01:44:13Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![yllsson](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yllsson/32/178010_2.png) [@yllsson](https://forum.freecodecamp.org/u/yllsson)\
**Post date:** [October 7, 2020, 7:17pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/18 "2020-10-07T19:17:19Z")

</div>

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! 😄

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [October 7, 2020, 7:19pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/19 "2020-10-07T19:19:06Z")

</div>

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

Happy coding!

---

<div class="post-metadata">

**Author:** ![jamlopez](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jamlopez/32/183882_2.png) [@jamlopez](https://forum.freecodecamp.org/u/jamlopez)\
**Post date:** [October 23, 2020, 5:58pm UTC](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630/20 "2020-10-23T17:58:05Z")

</div>

This was an awesome explanation, thanks!

[Next page](https://forum.freecodecamp.org/t/i-dont-get-the-range-concept-math-random-max-min-1-min/170630.md?page=2)
