2018-10-10 18:03:03 -04:00
---
id: cf1111c1c12feddfaeb2bdef
title: Generate Random Whole Numbers within a Range
challengeType: 1
2019-08-28 16:26:13 +03:00
videoUrl: https://scrimba.com/c/cm83yu6
forumTopicId: 18187
2018-10-10 18:03:03 -04:00
localeTitle: Генерировать случайные целые числа в пределах диапазона
---
## Description
2019-08-28 16:26:13 +03:00
< section id = 'description' >
Вместо того, чтобы генерировать случайное число между нулем и заданным числом, как это было раньше, мы можем сгенерировать случайное число, которое попадает в диапазон двух конкретных чисел. Для этого определим минимальное число < code > min< / code > и максимальное число < code > max< / code > . Вот формула, которую мы будем использовать. Найдите минутку, чтобы прочитать е е и попытайтесь понять, что делает этот код: < code > Math.floor(Math.random() * (max - min + 1)) + min< / code >
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2019-08-28 16:26:13 +03:00
< section id = 'instructions' >
Создайте функцию < code > randomRange< / code > которая принимает диапазон < code > myMin< / code > и < code > myMax< / code > и возвращает случайное число, которое больше или равно < code > myMin< / code > , и меньше или равно < code > myMax< / code > включительно.
< / section >
2018-10-10 18:03:03 -04:00
## Tests
< section id = 'tests' >
```yml
tests:
2019-08-28 16:26:13 +03:00
- text: The lowest random number that can be generated by < code > randomRange</ code > should be equal to your minimum number, < code > myMin</ code > .
testString: assert(calcMin === 5);
- text: The highest random number that can be generated by < code > randomRange</ code > should be equal to your maximum number, < code > myMax</ code > .
testString: assert(calcMax === 15);
- text: The random number generated by < code > randomRange</ code > should be an integer, not a decimal.
testString: assert(randomRange(0,1) % 1 === 0 );
- text: < code > randomRange</ code > should use both < code > myMax</ code > and < code > myMin</ code > , and return a random number in your range.
testString: assert((function(){if(code.match(/myMax/g).length > 1 & & code.match(/myMin/g).length > 2 & & code.match(/Math.floor/g) & & code.match(/Math.random/g)){return true;}else{return false;}})());
2018-10-10 18:03:03 -04:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
// 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 0; // Change this line
}
// Change these values to test your function
var myRandom = randomRange(5, 15);
```
< / div >
2019-08-28 16:26:13 +03:00
### After Tests
2018-10-10 18:03:03 -04:00
< div id = 'js-teardown' >
```js
2019-08-28 16:26:13 +03:00
var calcMin = 100;
var calcMax = -100;
for(var i = 0; i < 100 ; i + + ) {
var result = randomRange(5,15);
calcMin = Math.min(calcMin, result);
calcMax = Math.max(calcMax, result);
}
(function(){
if(typeof myRandom === 'number') {
return "myRandom = " + myRandom;
} else {
return "myRandom undefined";
}
})()
2018-10-10 18:03:03 -04:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-08-28 16:26:13 +03:00
function randomRange(myMin, myMax) {
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
}
2018-10-10 18:03:03 -04:00
```
2019-08-28 16:26:13 +03:00
2018-10-10 18:03:03 -04:00
< / section >