知识碎片 —— Math.random()

今天分享的是一个碎片知识,有关Java Script 中的 Math.random(),起初看到random,心里明白是生成随机数的,但是不知道这个方法生成随机数的范围,于是上W3C上搜了一下。

Math.random() 返回 0.0 到 1.0 之间的double数值的随机数。

 

若想返回整数则可以与Math.floor() ,Math.ceil() , parseInt()进行四舍五入处理。

 

parseInt()和 Math.floor()的效果都是一样的,都是向下取整数部分, 所以 parseInt(Math.random()*5,10),Math.floor(Math.random()*5) 则是生成 0-4 之间的随机数,而 Math.ceil(Math.random()*5) 则是生成1-5之间的随机数。

 

posted @ 2020-03-20 22:35  快乐柠檬  阅读(167)  评论(0编辑  收藏  举报