javascript 生成随机数

基础知识

向上取整

Math.ceil();
// 示例
Math.ceil(1.5); // 2
Math.ceil(2.4); // 3

向下取整

Math.floor();
// 示例
Math.floor(1.2); // 1
Math.floor(0.5); // 0

四舍五入

Math.round();
// 示例
Math.round(1.2); // 1
Math.round(0.5); // 1

0.0 ~ 1.0 之间的一个伪随机数。【包含0不包含1】

Math.random(); // 0.8647578968666494

获取从1到10的随机整数

Math.ceil(Math.random()*10); // 取0的概率极小。

生成[n,m]的随机整数

函数功能:生成[n,m]的随机整数。 在js生成验证码或者随机选中一个选项时很有用

//生成从minNum到maxNum的随机数
function randomNum(minNum,maxNum){
 switch(arguments.length){
 case 1:
 return parseInt(Math.random()*minNum+1,10);
 break;
 case 2:
 return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
 break;
 default:
 return 0;
 break;
 }
}

解析

Math.random() 生成[0,1)的数,所以

Math.random()*5 生成{0,5)的数。

通常期望得到整数,所以要对得到的结果处理一下。

parseInt(),Math.floor(),Math.ceil()和Math.round()都可得到整数。

parseInt()和Math.floor()结果都是向下取整。

所以Math.random()*5生成的都是[0,4]的随机整数。

所以生成[1,max]的随机数,公式如下:


Scan me!
JavaScript生成随机数, 来个抽奖活动
原创 web秀 2019-03-07 11:33:33
点击右上方红色按钮关注“web秀”,让你真正秀起来

前言
最近在头条也是给小伙伴们送出3.8福利,由于是小用户,头条也没有给出抽奖活动的功能,所以必须自己来一个,顺道补充一下JS随机数的小知识。结尾附上本次抽奖代码,一次抽出中奖者,明天(3.812:00公布结果,也是第一次做出这样的活动,感谢大家的支持。没有参与的小伙伴还有一天时间,希望大家可以参与。

JavaScript生成随机数, 来个抽奖活动
JavaScript生成随机数, 来个抽奖活动

基础知识
向上取整

Math.ceil();
// 示例
Math.ceil(1.5); // 2
Math.ceil(2.4); // 3
向下取整

Math.floor();
// 示例
Math.floor(1.2); // 1
Math.floor(0.5); // 0
四舍五入

Math.round();
// 示例
Math.round(1.2); // 1
Math.round(0.5); // 1
0.0 ~ 1.0 之间的一个伪随机数。【包含0不包含1】

Math.random(); // 0.8647578968666494
获取从110的随机整数

Math.ceil(Math.random()*10); // 取0的概率极小。
均衡获取01的随机整数

Math.round(Math.random());
均衡获取09的随机整数

Math.floor(Math.random()*10);
均衡获取010的随机整数

Math.round(Math.random()*10); // 其中获取最小值0和最大值10的几率少一半。因为结果在0~0.4 为0,0.5到1.4为1...8.5到9.4为9,9.5到9.9为10。所以头尾的分布区间只有其他数字的一半。
生成[n,m]的随机整数
函数功能:生成[n,m]的随机整数。 在js生成验证码或者随机选中一个选项时很有用

//生成从minNum到maxNum的随机数
function randomNum(minNum,maxNum){
 switch(arguments.length){
 case 1:
 return parseInt(Math.random()*minNum+1,10);
 break;
 case 2:
 return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
 break;
 default:
 return 0;
 break;
 }
}
解析

Math.random() 生成[0,1)的数,所以

Math.random()*5 生成{0,5)的数。

通常期望得到整数,所以要对得到的结果处理一下。

parseInt(),Math.floor(),Math.ceil()和Math.round()都可得到整数。

parseInt()和Math.floor()结果都是向下取整。

所以Math.random()*5生成的都是[0,4]的随机整数。

所以生成[1,max]的随机数,公式如下:

// max - 期望的最大值
parseInt(Math.random()*max,10)+1;
Math.floor(Math.random()*max)+1;
Math.ceil(Math.random()*max);

所以生成[0,max]到任意数的随机数,公式如下:

// max - 期望的最大值
parseInt(Math.random()*(max+1),10);
Math.floor(Math.random()*(max+1));

所以希望生成[min,max]的随机数,公式如下

// max - 期望的最大值
// min - 期望的最小值
parseInt(Math.random()*(max-min+1)+min,10);
Math.floor(Math.random()*(max-min+1)+min);

抽奖代码

 // 随机数函数
        function randomNum(minNum, maxNum) {
            switch (arguments.length) {
                case 1:
                    return parseInt(Math.random() * minNum + 1, 10);
                    break;
                case 2:
                    return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
                    break;
                default:
                    return 0;
                    break;
            }
        }
        // 参与抽奖人员
        let personnel = ['张三', '李四', '王五', '赵六'];
        let luckyIndex = randomNum(0, personnel.length);
        let luckyName = personnel[luckyIndex];
        console.log('恭喜 ' + luckyName + ' 中奖,谢谢您的支持');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值