ITPub博客

首页 > IT职业 > IT职场 > js 随机数,Math random

js 随机数,Math random

IT职场 作者:corespeed 时间:2021-09-09 08:57:57 0 删除 编辑

1.Math.random(); 结果为0-1间的一个随机数(包括0,不包括1)
2.Math.floor(num); 参数num为一个数值,函数结果为num的整数部分。
3.Math.round(num); 参数num为一个数值,函数结果为num四舍五入后的整数。

Math:数学对象,提供对数据的数学计算。
Math.random(); 返回0和1间(包括0,不包括1)的一个随机数。

Math.ceil(n); 返回大于等于n的最小整数。
用Math.ceil(Math.random()*10);时,主要获取1到10的随机整数,取0的几率极小。

Math.round(n); 返回n四舍五入后整数的值。
用Math.round(Math.random());可均衡获取0到1的随机整数。
用Math.round(Math.random()*10);时,可基本均衡获取0到10的随机整数,其中获取最小值0和最大值10的几率少一半。

Math.floor(n); 返回小于等于n的最大整数。
用Math.floor(Math.random()*10);时,可均衡获取0到9的随机整数。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4440/viewspace-2798284/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-09-09

  • 博文量
    90
  • 访问量
    1012