Instead of generating a random number between zero and a given number like we did before, we can generate a random number that falls within a range of two specific numbers.
To do this, we'll define a minimum number min and a maximum number max.
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
Instructions Create a function called randomRange that takes a range myMin and myMax and returns a random number that's greater than or equal to myMin, and is less than or equal to myMax, inclusive.
// 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; /*Math.floor - округляет результат.
Мы отинмаем 15-5, чтобы получить десять - нужный интервал в котром мы должны получить числы и добавляем 1,
потому что Math.random() выдаст на единицу меньше( от 20 - 19, от 10 - 9, так он работает).
И добавляем ко всему - минимум, чтобы он был включенным в промежуток. Тое сть,
генерируй числа от 5 еще 10 чисел - до 15, собственно, ест*/
}
// Change these values to test your function
var myRandom = randomRange(5, 15);