# Concept of Basic JavaScript: Generate Random Whole Numbers within a RangePassed

**URL:** <https://forum.freecodecamp.org/t/concept-of-basic-javascript-generate-random-whole-numbers-within-a-rangepassed/339089>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [January 7, 2020, 9:26am UTC](https://forum.freecodecamp.org/t/concept-of-basic-javascript-generate-random-whole-numbers-within-a-rangepassed/339089 "2020-01-07T09:26:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ametthey](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ametthey/32/46890_2.png) [@ametthey](https://forum.freecodecamp.org/u/ametthey)\
**Post date:** [January 7, 2020, 9:26am UTC](https://forum.freecodecamp.org/t/concept-of-basic-javascript-generate-random-whole-numbers-within-a-rangepassed/339089/1 "2020-01-07T09:26:45Z")

</div>

I pass the test of the exercice but I am not entirely sure to understand what is the use of the formula

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

Can someone explain to me ?

---

<div class="post-metadata">

**Author:** ![pjonp](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pjonp/32/131704_2.png) [@pjonp](https://forum.freecodecamp.org/u/pjonp)\
**Post date:** [January 7, 2020, 10:05am UTC](https://forum.freecodecamp.org/t/concept-of-basic-javascript-generate-random-whole-numbers-within-a-rangepassed/339089/2 "2020-01-07T10:05:28Z")

</div>

Here is the solution explanation for this challenge, does that make sense or which part are you unsure about? It is pretty tricky how the formula gets solved.

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

> [@freeCodeCamp Challenge Guide: Generate Random Whole Numbers within a Range](http://forum.freecodecamp.org/t/freecodecamp-challenge-guide-generate-random-whole-numbers-within-a-range/18187):
>
> [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. Solutionsplay_button Solu…

---

<div class="post-metadata">

**Author:** ![TomPringle](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tompringle/32/127949_2.png) [@TomPringle](https://forum.freecodecamp.org/u/TomPringle)\
**Post date:** [January 15, 2020, 1:38pm UTC](https://forum.freecodecamp.org/t/concept-of-basic-javascript-generate-random-whole-numbers-within-a-rangepassed/339089/3 "2020-01-15T13:38:03Z")

</div>

Thanks for explaining it out pjonp, but like ametthey I’m still confused about the formula.

Why is it necessary to run a `+ 1` to the min to begin with and then `+ min` again at the end of the formula?

I understand the concept of setting a min and a max, but why do we add another one to the total and then add the min again?

Tom

---

<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 15, 2020, 1:47pm UTC](https://forum.freecodecamp.org/t/concept-of-basic-javascript-generate-random-whole-numbers-within-a-rangepassed/339089/4 "2020-01-15T13:47:33Z")

</div>

`max - min + 1` gives the range width  
let’s say we want a number between 5 and 10: `10 - 5` is `5`, but we want one of six numbers: 5, 6, 7, 8, 9, 10 (that’s why we need `+1`)  
so, `Math.random()` gives a random number between 0 and 1, then multiplied by the range and rounded gives a number between 0 and 5  
so we add `min` to have a number between 5 and 10 as desired

---

<div class="post-metadata">

**Author:** ![TomPringle](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tompringle/32/127949_2.png) [@TomPringle](https://forum.freecodecamp.org/u/TomPringle)\
**Post date:** [January 15, 2020, 1:58pm UTC](https://forum.freecodecamp.org/t/concept-of-basic-javascript-generate-random-whole-numbers-within-a-rangepassed/339089/5 "2020-01-15T13:58:11Z")

</div>

Perfect, that makes sense. Thank you!

---

<div class="post-metadata">

**Author:** ![mailmeatshraddha](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@mailmeatshraddha](https://forum.freecodecamp.org/u/mailmeatshraddha)\
**Post date:** [May 27, 2020, 1:45am UTC](https://forum.freecodecamp.org/t/concept-of-basic-javascript-generate-random-whole-numbers-within-a-rangepassed/339089/6 "2020-05-27T01:45:06Z")

</div>

Thanks for the explanation…!!

---

<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:** [June 1, 2021, 12:53pm UTC](https://forum.freecodecamp.org/t/concept-of-basic-javascript-generate-random-whole-numbers-within-a-rangepassed/339089/7 "2021-06-01T12:53:20Z")

</div>

Closed on June 1, 2021, 12:53pm UTC


