js生成[n,m]的随机整数
发布时间:2022-07-26, 16:04:15 分类:HTML | 编辑 off 网址 | 辅助
正文 462字数 220,447阅读
生成 [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; 
    } 
} Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
			
			
			
			
		
有过 1 条评论 »
function shuffle (array) { if (array.length === 0 || array.length === 1){ return array } const newArr = array.slice() let top = newArr.length while (--top) { const c = Math.floor(Math.random() * (top + 1)) const t = newArr[c] newArr[c] = newArr[top] newArr[top] = t } return newArr }