2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: cf1111c1c12feddfaeb1bdef
|
2021-03-04 09:49:46 -08:00
|
|
|
|
title: 使用 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-03-04 09:49:46 -08:00
|
|
|
|
生成随机小数很棒,但随机数更有用的地方在于生成随机整数。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-04 09:49:46 -08:00
|
|
|
|
<ol><li>用 <code>Math.random()</code> 生成一个随机小数。</li><li>把这个随机小数乘以 <code>20</code>。</li><li>用 <code>Math.floor()</code> 向下取整,获得它最近的整数。</li></ol>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-04 09:49:46 -08:00
|
|
|
|
记住 `Math.random()` 永远不会返回 `1`。同时因为我们是在向下取整,所以最终我们获得的结果不可能有 `20`。 这确保了我们获得了一个在 `0` 到 `19` 之间的整数。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-04 09:49:46 -08:00
|
|
|
|
把操作连缀起来,代码类似于下面:
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-04-06 00:04:04 +09:00
|
|
|
|
```js
|
|
|
|
|
Math.floor(Math.random() * 20);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-04 09:49:46 -08:00
|
|
|
|
我们先调用 `Math.random()`,把它的结果乘以 20,然后把上一步的结果传给 `Math.floor()`,最终通过向下取整获得最近的整数。
|
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-04-06 00:04:04 +09:00
|
|
|
|
使用这个方法生成并返回 `0` 和 `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-03-04 09:49:46 -08:00
|
|
|
|
`randomWholeNum` 的结果应该是一个整数。
|
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-04-06 00:04:04 +09:00
|
|
|
|
应该使用 `Math.random` 生成一个随机数字。
|
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-03-04 09:49:46 -08:00
|
|
|
|
应该将 `Math.random` 的结果乘以 10,以生成 0 到 9 之间的随机数。
|
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-04-06 00:04:04 +09:00
|
|
|
|
应该使用 `Math.floor` 来删除数字的十进制部分。
|
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);
|
|
|
|
|
}
|
|
|
|
|
```
|