2018-10-10 18:03:03 -04:00
---
id: cf1111c1c12feddfaeb2bdef
2021-02-06 04:42:36 +00:00
title: Generate Random Whole Numbers within a Range
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/cm83yu6'
forumTopicId: 18187
2021-01-13 03:31:00 +01:00
dashedName: generate-random-whole-numbers-within-a-range
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00: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-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
To do this, we'll define a minimum number `min` and a maximum number `max` .
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Here's the formula we'll use. Take a moment to read it and try to understand what this code is doing:
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
`Math.floor(Math.random() * (max - min + 1)) + min`
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
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.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The lowest random number that can be generated by `randomRange` should be equal to your minimum number, `myMin` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(calcMin === 5);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The highest random number that can be generated by `randomRange` should be equal to your maximum number, `myMax` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(calcMax === 15);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The random number generated by `randomRange` should be an integer, not a decimal.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(randomRange(0, 1) % 1 === 0);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`randomRange` should use both `myMax` and `myMin` , and return a random number in your range.
2020-04-29 18:29:13 +08:00
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
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
```
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --after-user-code--
```js
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";
}
})()
```
## --seed-contents--
```js
function randomRange(myMin, myMax) {
// Only change code below this line
return 0;
// Only change code above this line
}
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
function randomRange(myMin, myMax) {
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
}
```