javascript - 如何理解 JavaScript 'Math.floor(Math.random() *6)+1' 中的这个内置函数?
问题描述
如果我使用此代码:
Math.floor(Math.random() *6)
这会给我一个从 1 到 5 的随机数,对吗?
编写此代码是否相同:
Math.floor(Math.random() *6)+1
像这样?
Math.floor(Math.random() *7)
因为他们都会给我一个介于 1 到 6 之间的数字?
只是看一个教程,他没有解释这个功能,我很好奇他是否可以使用第二个例子而不是第一个例子。
解决方案
不,它们不会是相同的(6 是最大值)Math.floor(Math.random() *6)+1
之间的随机值。这个最小值始终是我们最后添加的值。1-6
1
1
Math.floor(Math.random() *7)
是之间的随机值0-7
推荐阅读
- git - 使用两个不同的 Github 帐户
- python - 将列表值合并到 1 个字符串中
- regex - Python:删除所有以大写字母开头且标点符号后不出现的单词
- html - 如何在angular6中将记录从一个网格推送到另一个网格
- typescript - Mongoose 的 loadClass() 和 TypeScript
- c++ - envp 代表什么?
- typescript - 路由器子组件未绑定
- python-3.x - 如何使用 PIL 将多行文本水平和垂直居中
- php - 如何检查结果中可用或不可用的数据?
- java - 组件返回失败代码:0x80520012 (NS_ERROR_FILE_NOT_FOUND) [amIAddonManagerStartup.readStartupData] 与 GeckoDriver Mozilla Selenium Java