JS:var die = Math.floor(Math.random()*6 + 1); 为什么die的值会在1和6之间?

来源:学生作业帮助网 编辑:作业帮 时间:2024/03/28 17:23:22

JS:var die = Math.floor(Math.random()*6 + 1); 为什么die的值会在1和6之间?

Math.random() 生成0和1之间的随机小数
Math.random() * 6 生成0和6之间的随机小数
Math.random() * 6 + 1生成1和7之间的随机小数
Math.floor(x)函数,返回小于等于x的最大整数
所以,Math.floor(Math.random() * 6 + 1)生成1和7之间的随机整数(不包括7)