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

来自http://tunps.com/javascript-random-number-generation

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

About tunpishuang

just 4 fun·····
This entry was posted in 未分类 and tagged . Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>