javascript模拟掷骰子生成1到6的随机数

var diceThrow=Math.round(Math.random()*5)+1;
document.write("You threw a " + diceThrow);

需要注意的是
0=< random() <1
round()四舍五入,比如round(1.4)=1 ,round(1.5)=2
ceil()总是向上取近似值,比如ceil(0.00000000001)=1
floor()总是向上取近似值,比如floor(0.9999999999999)=0

标签: JavaScript

添加新评论