2018-09-30 23:01:58 +01:00
---
id: cf1111c1c12feddfaeb2bdef
title: Generate Random Whole Numbers within a Range
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cm83yu6'
2019-07-31 11:32:23 -07:00
forumTopicId: 18187
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
2020-05-29 02:56:17 -06:00
Instead of generating a random whole number between zero and a given number like we did before, we can generate a random whole number that falls within a range of two specific numbers.
2018-09-30 23:01:58 +01:00
To do this, we'll define a minimum number < code > min< / code > and a maximum number < code > max< / code > .
Here's the formula we'll use. Take a moment to read it and try to understand what this code is doing:
< code > Math.floor(Math.random() * (max - min + 1)) + min< / code >
< / section >
## Instructions
< section id = 'instructions' >
2020-05-29 02:56:17 -06:00
Create a function called < code > randomRange< / code > that takes a range < code > myMin< / code > and < code > myMax< / code > and returns a random whole number that's greater than or equal to < code > myMin< / code > , and is less than or equal to < code > myMax< / code > , inclusive.
2018-09-30 23:01:58 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2018-10-20 21:02:47 +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 > .
2019-07-13 00:07:53 -07:00
testString: assert(calcMin === 5);
2018-10-20 21:02:47 +03:00
- text: The highest random number that can be generated by < code > randomRange</ code > should be equal to your maximum number, < code > myMax</ code > .
2019-07-13 00:07:53 -07:00
testString: assert(calcMax === 15);
2018-10-20 21:02:47 +03:00
- text: The random number generated by < code > randomRange</ code > should be an integer, not a decimal.
2019-07-13 00:07:53 -07:00
testString: assert(randomRange(0,1) % 1 === 0 );
2018-10-20 21:02:47 +03:00
- text: < code > randomRange</ code > should use both < code > myMax</ code > and < code > myMin</ code > , and return a random number in your range.
2019-07-13 00:07:53 -07:00
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-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function randomRange(myMin, myMax) {
2020-03-25 08:07:13 -07:00
// Only change code below this line
return 0;
// Only change code above this line
2018-09-30 23:01:58 +01:00
}
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2018-10-20 21:02:47 +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-09-30 23:01:58 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function randomRange(myMin, myMax) {
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
}
```
< / section >