2018-10-10 18:03:03 -04:00
---
id: cf1111c1c12feddfaeb1bdef
2021-02-06 04:42:36 +00:00
title: Generate Random Whole Numbers with JavaScript
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/cRn6bfr'
forumTopicId: 18186
2021-01-13 03:31:00 +01:00
dashedName: generate-random-whole-numbers-with-javascript
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00: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.
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00: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 >
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00: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-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Putting everything together, this is what our code looks like:
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
`Math.floor(Math.random() * 20);`
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
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.
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
Use this technique to generate and return a random whole number between `0` and `9` .
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 result of `randomWholeNum` should be a whole number.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(
typeof randomWholeNum() === 'number' & &
(function () {
var r = randomWholeNum();
return Math.floor(r) === r;
})()
);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should use `Math.random` to generate a random number.
2018-10-10 18:03:03 -04:00
```js
2021-02-06 04:42:36 +00:00
assert(code.match(/Math.random/g).length >= 1);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
You should have multiplied the result of `Math.random` by 10 to make it a number that is between zero and nine.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```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-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should use `Math.floor` to remove the decimal part of the number.
2020-04-29 18:29:13 +08:00
2018-10-10 18:03:03 -04:00
```js
2021-02-06 04:42:36 +00:00
assert(code.match(/Math.floor/g).length >= 1);
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
(function(){return randomWholeNum();})();
```
## --seed-contents--
```js
function randomWholeNum() {
// Only change code below this line
return Math.random();
}
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
function randomWholeNum() {
return Math.floor(Math.random() * 10);
}
```