# Basic JavaScript - Generate Random Whole Numbers within a Range

Tell us what’s happening:
Hello there. I am stuck here:

“Here’s the formula we’ll use. Take a moment to read it and try to understand what this code is doing:”

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

Could someone break down this formula for me? I’m having trouble reading this formula

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

Challenge: Basic JavaScript - Generate Random Whole Numbers within a Range

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

I took a moment to read it and tried to understand what this code is doing, but I can’t understand what this code is doing.

I think it’s doing this:

console.log(Math.floor(Math.random() * (15- 5 + 1)) + 5), which is 16

No. It will not ever produce 16. Let me break it down in smaller steps:

``````Math.floor(Math.random() * (15 - 5 + 1)) + 5
``````
``````Math.floor(Math.random() * (11)) + 5
``````

Math.random() will produce a number between `0` and `1` (including `0` but not including `1`). This means, this will always be a fraction of some kind (or zero). In this case, the fraction would be multiplied by `11`. Let’s say for example `Math.random()` returned `0.3`, then you would have:

``````Math.floor(0.3 * (11)) + 3
``````
``````Math.floor(3.3) + 3
``````

Math.floor returns removes the fraction portion of a number, so in this case, you are left with just `3`

``````3 + 3 = 6
``````

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