2018-09-30 23:01:58 +01:00
---
id: cf1111c1c12feddfaeb1bdef
title: Generate Random Whole Numbers with JavaScript
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cRn6bfr'
2019-07-31 11:32:23 -07:00
forumTopicId: 18186
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
It's great that we can generate random decimal numbers, but it's even more useful if we use it to generate random whole numbers.
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
< ol > < li > Use < code > Math.random()< / code > to generate a random decimal.< / li > < li > Multiply that random decimal by < code > 20< / code > .< / li > < li > Use another function, < code > Math.floor()< / code > to round the number down to its nearest whole number.< / li > < / ol >
2020-11-27 19:02:05 +01:00
Remember that `Math.random()` can never quite return a `1` and, because we're rounding down, it's impossible to actually get `20` . This technique will give us a whole number between `0` and `19` .
2018-09-30 23:01:58 +01:00
Putting everything together, this is what our code looks like:
2020-11-27 19:02:05 +01:00
`Math.floor(Math.random() * 20);`
We are calling `Math.random()` , multiplying the result by 20, then passing the value to `Math.floor()` function to round the value down to the nearest whole number.
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Use this technique to generate and return a random whole number between `0` and `9` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
The result of `randomWholeNum` should be a whole number.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(
typeof randomWholeNum() === 'number' & &
(function () {
var r = randomWholeNum();
return Math.floor(r) === r;
})()
);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
You should use `Math.random` to generate a random number.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(code.match(/Math.random/g).length >= 1);
```
You should have multiplied the result of `Math.random` by 10 to make it a number that is between zero and nine.
```js
assert(
code.match(/\s*?Math.random\s*?\(\s*?\)\s*?\*\s*?10[\D]\s*?/g) ||
code.match(/\s*?10\s*?\*\s*?Math.random\s*?\(\s*?\)\s*?/g)
);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
You should use `Math.floor` to remove the decimal part of the number.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(code.match(/Math.floor/g).length >= 1);
```
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --after-user-code--
2018-09-30 23:01:58 +01:00
```js
2018-10-20 21:02:47 +03:00
(function(){return randomWholeNum();})();
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
```js
function randomWholeNum() {
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
return Math.random();
}
```
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 randomWholeNum() {
return Math.floor(Math.random() * 10);
}
```