Как надо понимать этот код и для чего это нужно?

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

Looks like it is a function called randomRange which takes 2 inputs a min and max number.
It seems to return a random number between these two numbers, but to confirm you can add some console.log statements to the function and run it.

for eg you can change the function as follows:

let r = Math.random();
console.log(r);
let f = Math.floor(Math.random() * (myMax - myMin + 1) + myMin);
console.log(f);
return f;

Что обозначает (myMax - myMin + 1) + myMin)

Because the random() function returns a number between 0 and 1 (but never equal to 1),
if you want to turn the random number to a different range you have to manipulate that number somehow.
So for eg. let’s say random gave us a 0
and let us say we want the range to be between 10-20 and not 0-1 then how do we turn 0 to 10?

0 * (20-10 +1) +10
0 * (10+1) + 10
0 * (11) + 10
0 + 10
10

So the final result will be 10 if Math.random() gives 0

What if Math.random() gives 0.1 ?
Let’s calculate again:

0.1 * (20-10+1) + 10
0.1 * (10+1)+10
0.1*(11)+10
1.1+10
11.1
(and then apply Math.floor)
11

So the final result will be 11

etc. etc.

Только вот все ровно не понятно как это писать на коде.

I’m sorry I am not able to understand your last comment (the translation I see is not useful).
We have different language subforums in fCC.
If you want, you can try to move this topic to one of them (if one exists for your language).

If you cannot find a suitable subforum, then perhaps try to post a new topic but this time, I will not answer it, and perhaps someone else will see it who can answer in your language.

Sorry that is all the help I can offer at this time.

Ответы на мои вопросы получены не были.

As was said above, the last text you posted could not be automatically translated, so it was not possible for us to read or answer your question.

Мне не понятны объяснения кода,я не понимаю форму записи.

I don’t know what you mean by “entry form”.

It is easier if you give specific questions. What about the code do you not understand?

(myMax - myMin + 1) + myMin);
что это значит?

That is part the formula for creating a random number in between myMax and myMin. You cut off half of the formula, and it doesn’t really work correctly without the rest:

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

это мне не о чем не говорит такой ответ,(myMax - myMin + 1) + myMin);откуда это взялось?и что значит +1?

This is incomplete. That half of a formula means absolutely nothing on its own.

Please stop only using half of the formula - only half of the formula is useless.

This is the full formula. Only the full formula or logically complete parts of the full formula make sense.

Math.random() provides a random number between 0 and 1.

(myMax - myMin + 1) is the number of values in between myMin and myMax.

Math.random() * (myMax - myMin + 1) multiplying the two together gives a random number between 0 and (myMax - myMin + 1)

Math.random() * (myMax - myMin + 1) + myMin This turns the result into a random number between myMin and myMax

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

And adding Math.floor() turns the result into an integer instead of a decimal number.

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