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

来源:学生作业学帮网 编辑:学帮网 时间:2024/07/04 08:59:10

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)