js随机数 范围随机 整数

js常用方法

Math.ceil(n); //向上取整。返回>=n的最小整数。
Math.floor(n); //向下取整。返回为n的整数部分。
Math.round(n); //四舍五入的整数。
Math.random(n); //0.0 ~ 1.0 之间的一个伪随机数。【含0,不含1】
Math.ceil(Math.random()*10); // 1到10的随机整数 ,取0的概率极小。
Math.floor(Math.random()*10); //0到9的随机整数。
Math.round(Math.random()); //0或1的随机整数。
Math.round(Math.random()*10); //0到10的随机整数,0和10的几率少一半。


[1,max]

parseInt(Math.random()*max,10)+1;
Math.floor(Math.random()*max)+1;
Math.ceil(Math.random()*max);

[0,max]

parseInt(Math.random()(max+1),10);
Math.floor(Math.random()
(max+1));

[min,max]

parseInt(Math.random()(max-min+1)+min,10);
Math.floor(Math.random()
(max-min+1)+min);

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值