# How does this code work?

``````function randomRange(myMin, myMax) {
return Math.floor(Math.random() * (myMax - myMin + 1) + myMin);
}
``````

Если значения были`myMin = 1, myMax= 10` , одним из результатов могло быть следующее:

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`
I didn’t understand anything from the example, what does → mean?
where did a*b and c come from? Why write 10-1+1 like this if the answer is exactly 10? If 10 was added to the variable? Who cares?

It’s just a way of showing an equivalency. `10 - 1 + 1` → (is equivalent to) `10`

The `a` and `b` represent the operands of the `*` operator in the code. Those operands are `a=Math.random()` and `b=(myMax - myMin + 1)`

It’s a result of substituting the variables with their values. With the specific values of `myMin` and `myMax` you have, the `-1+1` cancels out so might seem pointless, but if you had different values for `myMin` and `myMax` the equation would be different.

Lets look at an example with some different numbers, `myMin=15`, `myMax=35`, and `Math.random()=0.824`.

1. `(myMax - myMin + 1)``(35 - 15 + 1)` → 21 (lets call this result `x`)
2. `Math.random() * x``0.824 * 21` → 17.241 (lets call this result `y`)
3. `y + myMin``17.241 + 15` → 32.241 (lets call this result `z`)
4. `Math.floor(z)``Math.floor(17.241)` → 17

The point of the equation is to calculate the highest value you can multiply the result of Math.random() by and be within the range specified. Imagine you went simple, and just did `Math.floow(Math.random() * myMax)`. This would work to give you result between 0 and `myMax`.

If you want to have a minimum higher than 0, like the 15 example above, you need to apply that minimum as an offset to the result. This is what the `+ myMin` part of the code does. If you only did this though, you would exceed the maximum if `Math.random()` returned a high result (eg: 0.824)

1. `Math.random() * myMax``0.824 * 35` → 28.84 (lets call this result `y`)
2. `y + myMin``28.84 + 15` → 43.84 (lets call this result `z`)
3. `Math.floor(z)``Math.floor(43.84)` → 48

You get a result of `48` which is outside the requested range of 15 - 35. Since `myMin` is getting added to the final result, you need to subtract it from the maximum to ensure the final result stays in the desired range. That is what the `(myMax - myMin + 1)` bit of the code does. The extra `+1` allows the maximum value to be a possible result.

strictly speaking, i think it’s meant to be “yields”, but yea thats quibbling on my part

Based on the number presented there, where you’ve got it listed 1,2,3…, they’ve got it a,b,c. so your list’s 1 = a, 2 = b, etc. and they’re building up the code line from pieces.

So

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

(Note: I am adding extra parentheses to make it a bit more obvious what’s going on where.)

would be
` return e`

`e` would be #5: `Math.floor(d)`
`d` would be #4: `c + myMin`
`c` would be #3: `a * b`
`b` would be #2: `(myMax - myMin + 1)`
and `a` would be #1: `Math.random()`

I’m trying to calculate on a calculator to get 17, but I can’t do it. So how does this Javascript work?

Here I didn’t even understand what it was about, in what order to watch it, I see everything differently.

It’s generating a random whole number between myMax and myMin inclusively.

`Math.random()` will give a random float (a number with a decimal and fractional components) between 0 and 1 (but does NOT include 1).

`myMax-myMin+1` is the range of the span between myMax and myMin; for example, if myMin is 3 and myMax is 5, 5-3+1 = 3. There are 3 numbers in that range that can be picked (3,4,5).
Why do we add 1? Because Math.Random does not include 1, the most it could ever generate is 0.9999999… . But we want to include our endpoint, so we add 1 to make the range a bit longer.

Multiplying some number between 0 and 0.99999… (the first part) and the range of the span (the second part) will give you a floating point number somewhere between 0 and the range number. In our previous example, the range was 3, so it will give you a number somewhere between 0 and 2.999999… (again, we can get close to 3, but will never be 3)

Adding the minimum value shifts that 0…X range to be the numbers we want. In our example, myMin was 3. Adding 3 to the range 0-2.99999… will give us a new range 3-5.99999…

Floor knocks the decimal part off of the number. So in our example, for any value between 3 and 3.99999… the result will be 3, for any number between 4 and 4.99999… the result will be 4, and between 5 and 5.99999… the result will be 5. Thus, in our example, we have generated a random whole number between 3 and 5.

BTW this is standard mathematics and has nothing to do with JavaScript. So, if you do not understand this, you might get back to school and learn the basics of mathematics instead?

I take a shortcut in thinking sometimes. Does this code help me or not? I do not always need to understand exact how a car works under the hood, as long as it takes me from A to B. Sooner or later I grasp how it works.

Basically you get a random value based on the range you pass to the function. That is enough for me.

1 Like

Then it became clear to me

Yes, I’ll go to school tomorrow, I just bought a briefcase and textbooks for 1st grade today))

Ah so there is somebody who can explain JS after all

1 Like

This is a small part of everything that I understand, the rest is still to come)

Ah, so after something was achieved you moved the goal posts. Gee, I didn’t see that one coming.