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
---
2020-11-27 19:02:05 +01:00
# --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.
2020-11-27 19:02:05 +01:00
To do this, we'll define a minimum number `min` and a maximum number `max` .
2018-09-30 23:01:58 +01:00
Here's the formula we'll use. Take a moment to read it and try to understand what this code is doing:
2020-11-27 19:02:05 +01:00
`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 whole number that's greater than or equal to `myMin` , and is less than or equal to `myMax` , inclusive.
# --hints--
The lowest random number that can be generated by `randomRange` should be equal to your minimum number, `myMin` .
```js
assert(calcMin === 5);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
The highest random number that can be generated by `randomRange` should be equal to your maximum number, `myMax` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(calcMax === 15);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
The random number generated by `randomRange` should be an integer, not a decimal.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(randomRange(0, 1) % 1 === 0);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`randomRange` should use both `myMax` and `myMin` , and return a random number in your range.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
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
2020-11-27 19:02:05 +01:00
# --seed--
## --after-user-code--
2018-09-30 23:01:58 +01:00
```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
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
function randomRange(myMin, myMax) {
// Only change code below this line
return 0;
// Only change code above this line
}
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
function randomRange(myMin, myMax) {
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
}
```