# freeCodeCamp Challenge Guide: Generate Random Whole Numbers within a Range

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [July 7, 2016, 8:20pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187 "2016-07-07T20:20:19Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [July 7, 2016, 8:20pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/1 "2016-07-07T20:20:19Z")

</div>

# [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)

* * *

## Hints

### Hint 1

`randomRange` should use both `myMax` and `myMin`, and return a random number in your range.

You cannot pass the test if you are only re-using the function `ourRandomRange` inside your `randomRange` formula. You need to write your own formula that uses the variables `myMax` and `myMin`. It will do the same job as using `ourRandomRange`, but ensures that you have understood the principles of the `Math.floor()` and `Math.random()` functions.

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> function randomRange(myMin, myMax) {
> return Math.floor(Math.random() * (myMax - myMin + 1) + myMin);
> }
> 
> ```
> 
> #### Code Explanation
> 
> - `Math.random()` generates our random number between 0 and ≈ 0.9.
> - Before multiplying it, it resolves the part between parenthesis `(myMax - myMin + 1)` because of the grouping operator `( )`.
> - The result of that multiplication is followed by adding `myMin` and then “rounded” to the largest integer less than or equal to it (eg: 9.9 would result in 9)
> 
> If the values were `myMin = 1, myMax= 10`, one result could be the following:
> 
> 1. `Math.random() = 0.8244326990411024`
> 2. `(myMax - myMin + 1) = 10 - 1 + 1 -> 10`
> 3. `a * b = 8.244326990411024`
> 4. `c + myMin = 9.244326990411024`
> 5. `Math.floor(9.244326990411024) = 9`
> 
> #### Relevant Links
> 
> - [“Math.random()” - _MDN JavaScript reference_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random)
> - [“Math.floor()” - _MDN JavaScript reference_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor)
> - [“Grouping operator” - _MDN JavaScript reference_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Grouping)

---

<div class="post-metadata">

**Author:** ![lmackenz](https://avatars.discourse-cdn.com/v4/letter/l/ea5d25/32.png) [@lmackenz](https://forum.freecodecamp.org/u/lmackenz)\
**Post date:** [January 30, 2017, 10:37pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/2 "2017-01-30T22:37:03Z")

</div>

Thank you for taking the time to walk through this step by step. I had been able to generate a code answer -but couldn’t understand what the actual formula meant. Awesome. Again - thank you.

---

<div class="post-metadata">

**Author:** ![Finagl3](https://avatars.discourse-cdn.com/v4/letter/f/b4bc9f/32.png) [@Finagl3](https://forum.freecodecamp.org/u/Finagl3)\
**Post date:** [March 7, 2017, 2:04am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/3 "2017-03-07T02:04:47Z")

</div>

Yes, thank you for the explanation. It was easy to figure out what was required to pass the challenge, but I wasn’t comfortable passing this without understanding the formula first. This explained it very logically, and I probably wouldn’t have been able to figure it out on my own. – putting this explanation in my notes!

---

<div class="post-metadata">

**Author:** ![nickfuentes](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nickfuentes/32/93427_2.png) [@nickfuentes](https://forum.freecodecamp.org/u/nickfuentes)\
**Post date:** [March 20, 2017, 2:04am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/4 "2017-03-20T02:04:20Z")

</div>

Awesome work here man really appreciate the the simple explanation!

---

<div class="post-metadata">

**Author:** ![johncarnegie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/johncarnegie/32/64904_2.png) [@johncarnegie](https://forum.freecodecamp.org/u/johncarnegie)\
**Post date:** [May 2, 2017, 3:47am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/5 "2017-05-02T03:47:59Z")

</div>

Thanks. Now I understand why we need to use Max - Min + 1 instead of Max - Min.

---

<div class="post-metadata">

**Author:** ![AhmedMJ](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmedmj/32/60569_2.png) [@AhmedMJ](https://forum.freecodecamp.org/u/AhmedMJ)\
**Post date:** [June 30, 2017, 10:17am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/6 "2017-06-30T10:17:21Z")

</div>

I understand the part until the +1 to make the max inclusive, but I don’t understand the part after that, what is the point of it ?

---

<div class="post-metadata">

**Author:** ![jarrick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jarrick/32/161790_2.png) [@jarrick](https://forum.freecodecamp.org/u/jarrick)\
**Post date:** [July 5, 2017, 9:24pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/7 "2017-07-05T21:24:41Z")

</div>

Thank you for the detailed explanation. There is still one thing that does not add up for me.

How is `((max + 1) - min)) + min` different than `max + 1`?

If you are subtracting `min` and then adding it back in, doesn’t that cancel out and leave you with `max + 1`?

---

<div class="post-metadata">

**Author:** ![Russellmania91](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/russellmania91/32/39160_2.png) [@Russellmania91](https://forum.freecodecamp.org/u/Russellmania91)\
**Post date:** [July 8, 2017, 4:11am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/8 "2017-07-08T04:11:54Z")

</div>

It is different because of the parenthesis. If Max is 6 and Min is 2, you get ((6+1) - 2)) + 2

And THIS is how the computer works it out:

Math.floor(Math.random)_((6+1)-2)+2  
=Math.floor(Math.random)_(5)+2  
= 0+2…1+2…2+2…3+2…4+2  
=2…3…4…5…6

---

<div class="post-metadata">

**Author:** ![pineapple](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pineapple/32/161740_2.png) [@pineapple](https://forum.freecodecamp.org/u/pineapple)\
**Post date:** [July 13, 2017, 1:53am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/9 "2017-07-13T01:53:14Z")

</div>

Given `var myRandom = randomRange(2, 6);`

Lets start with `Math.floor(Math.random() \* (6 + 1))’ because we can agree that it returns 0-6.

However! If `Math.random()` returned `0` then `Math.floor(0 * (6 + 1))` would return **0** which is outside of our given range of 2 through 6 🙁 : .

So the next logical step is to add two after all the multiplication has been executed. Which would look like: `Math.floor(Math.random() * (myMax + 1)) + myMin`

However! If `Math.random()` returned `6.99999` then `Math.floor(6.99999 * (6 + 1)) + 2` would return **8!** which is outside of our given range of 2 through 6. no good 🙁

SOOO we have to subtract the min from our max then add one to guarantee that the floor of random \* our ( 6 - 2) is no greater than 4 that way we can still add the min at the end incase Math.random generates a `0` .

Essentially what this does is gets a number between 0 and 4 and adds 2 to the number. Which is equal to the range 2 through 6.

In other words.

The range of 0 through 4 plus 2 is equal to the range 2 through 6.

---

<div class="post-metadata">

**Author:** ![pineapple](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pineapple/32/161740_2.png) [@pineapple](https://forum.freecodecamp.org/u/pineapple)\
**Post date:** [July 13, 2017, 2:01am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/10 "2017-07-13T02:01:40Z")

</div>

Given `var myRandom = randomRange(2, 6);`

Lets start with `Math.floor(Math.random() \* (6 + 1))’ because we can agree that it returns 0-6.

However! If `Math.random()` returned `0` then `Math.floor(0 * (6 + 1))` would return **0** which is outside of our given range of 2 through 6 🙁 : .

So the next logical step is to add two after all the multiplication has been executed. Which would look like: `Math.floor(Math.random() * (myMax + 1)) + myMin`

However! If `Math.random()` returned `6.99999` then `Math.floor(6.99999 * (6 + 1)) + 2` would return **8!** which is outside of our given range of 2 through 6. no good 🙁

SOOO we have to subtract the min from our max then add one to guarantee that the floor of random \* our ( 6 - 2) is no greater than 4 that way we can still add the min at the end incase Math.random generates a `0` .

Essentially what this does is gets a number between 0 and 4 and adds 2 to the number. Which is equal to the range 2 through 6.

In other words.

The range of 0 through 4 plus 2 is equal to the range 2 through 6.

---

<div class="post-metadata">

**Author:** ![yankov1984](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yankov1984/32/58233_2.png) [@yankov1984](https://forum.freecodecamp.org/u/yankov1984)\
**Post date:** [July 15, 2017, 8:34am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/11 "2017-07-15T08:34:25Z")

</div>

My solution

return Math.floor(Math.random() \* ((myMax + 1) - myMin)) + myMin;

---

<div class="post-metadata">

**Author:** ![Y134](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/y134/32/70357_2.png) [@Y134](https://forum.freecodecamp.org/u/Y134)\
**Post date:** [July 15, 2017, 4:11pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/12 "2017-07-15T16:11:07Z")

</div>

Thank you. This explains it best. I appreciate your explanation. 🕶

---

<div class="post-metadata">

**Author:** ![Y134](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/y134/32/70357_2.png) [@Y134](https://forum.freecodecamp.org/u/Y134)\
**Post date:** [July 15, 2017, 4:26pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/14 "2017-07-15T16:26:03Z")

</div>

How did the middle (myMax - myMin + 1) from the original formula become ((myMax + 1) - myMin))? This is the part I am confused about.

---

<div class="post-metadata">

**Author:** ![bahia0019](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bahia0019/32/161628_2.png) [@bahia0019](https://forum.freecodecamp.org/u/bahia0019)\
**Post date:** [July 19, 2017, 3:03am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/15 "2017-07-19T03:03:56Z")

</div>

I’m finding it very frustrating to have to come to the “Hints” section to learn the skills I should be learning in the lesson. If it’s critical to solving the problems, shouldn’t we learn it prior to the problem?

---

<div class="post-metadata">

**Author:** ![cktai](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cktai/32/130465_2.png) [@cktai](https://forum.freecodecamp.org/u/cktai)\
**Post date:** [July 19, 2017, 2:17pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/16 "2017-07-19T14:17:44Z")

</div>

I feel as the same as you. This lesson doesn’t have enough explanation to the code.

---

<div class="post-metadata">

**Author:** ![rugano](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rugano/32/93045_2.png) [@rugano](https://forum.freecodecamp.org/u/rugano)\
**Post date:** [July 25, 2017, 8:39am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/17 "2017-07-25T08:39:49Z")

</div>

You may approach it like this:

// Example  
function ourRandomRange(ourMin, ourMax) {

return Math.floor(Math.random() \* (ourMax - ourMin + 1)) + ourMin;  
}

ourRandomRange(1, 9);

// Only change code below this line.

function randomRange(myMin, myMax) {

return Math.floor(Math.random() \* (myMax - myMin + 1)) + myMin; // Changed line

}

randomRange();

// Change these values to test your function  
var myRandom = randomRange(5, 15);

---

<div class="post-metadata">

**Author:** ![kiritoMasamune](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@kiritoMasamune](https://forum.freecodecamp.org/u/kiritoMasamune)\
**Post date:** [August 4, 2017, 6:54pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/18 "2017-08-04T18:54:36Z")

</div>

Agreed. Makes you think somethings wrong with you doesn’t it?

---

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 10, 2019, 1:19am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/20 "2019-08-10T01:19:45Z")

</div>



---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 7, 2025, 12:00pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187/21 "2025-01-07T12:00:35Z")

</div>


