js中Math之random,round,ceil,floor的用法总结

2022-01-15,,,,

本篇文章是对js中Math之random,round,ceil,floor用法进行了总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助

1.Math.random(); 结果为0-1间的一个随机数(包括0,不包括1)  
Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) [html] view plaincopyprint?
 

2.Math.floor(num); 参数num为一个数值,函数结果为num的整数部分(返回小于等于n的最大整数)。 

Math.floor(num); 参数num为一个数值,函数结果为num的整数部分(返回小于等于n的最大整数)。[html] view plaincopyprint?
 

3.Math.round(num); 参数num为一个数值,函数结果为num四舍五入后的整数。 

Math.round(num); 参数num为一个数值,函数结果为num四舍五入后的整数。[html] view plaincopyprint?
 

4.Math.ceil(n); 返回大于等于n的最小整数。 

Math.ceil(n); 返回大于等于n的最小整数。[html] view plaincopyprint?
 

5.Math.ceil(Math.random()*10);时,主要获取1到10的随机整数,取0的几率极小。 

Math.ceil(Math.random()*10);时,主要获取1到10的随机整数,取0的几率极小。[html] view plaincopyprint?
 

6.Math.round(Math.random());可均衡获取0到1的随机整数。 

Math.round(Math.random());可均衡获取0到1的随机整数。[html] view plaincopyprint?
 

7.Math.round(Math.random()*10);时,可基本均衡获取0到10的随机整数,其中获取最小值0和最大值10的几率少一半。 

Math.round(Math.random()*10);时,可基本均衡获取0到10的随机整数,其中获取最小值0和最大值10的几率少一半。[html] view plaincopyprint?
 

8.Math.floor(Math.random()*10);时,可均衡获取0到9的随机整数。 

以上就是js中Math之random,round,ceil,floor的用法总结的详细内容,更多请关注本站其它相关文章!

《js中Math之random,round,ceil,floor的用法总结.doc》

下载本文的Word格式文档,以方便收藏与打印。