很好奇为什么math.floor比math.ceil我做随机词生成和签入时返回更好的结果console.log。
为什么不能math.ceil完美运行?是否存在math.ceil与与不兼容的问题,math.random或者是我如何分配字符串元素的数组编号(项目)?
Math.random() 返回一个小于 1(非包含)但大于 0(包含)的数字
Math.floor(Math.random() * 10); // returns a random integer from 0 to 9
Math.ceil(Math.random() * 10); // returns a random integer from 0 to 10 with a very low chance of 0
Run Code Online (Sandbox Code Playgroud)
如果Math.random结果0完全相同,则Math.floor()andMath.ceil()都将返回0,但如果Math.random()结果为0.00000001,则Math.floor()返回0并Math.ceil()返回1。
| 归档时间: |
|
| 查看次数: |
121 次 |
| 最近记录: |