randomNum () { let arr = [1, 2, 3] // 保证9位随机码包含数字+大写字母+小写字母 let code = '' function getRandom (min, max) { return Math.round(Math.random() * (max - min) + min) } function randomsort (a, b) { return Math.random() > 0.5 ? -1 : 1 // 用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1 } for (let i = 0; i < 6; i++) { //需要几位 就把i<6中的6 换成自己需要的个数 arr.push(getRandom(1, 3)) // 补成9位 } arr.sort(randomsort) // 打乱数组 for (let i = 0; i < 9; i++) { //需要几位 就把i<6中的6 换成自己需要的个数 let type = arr[i] switch (type) { case 1: code += String.fromCharCode(getRandom(48, 57)) // 数字 break case 2: code += String.fromCharCode(getRandom(65, 90)) // 大写字母 break case 3: code += String.fromCharCode(getRandom(97, 122)) // 小写字母 break } } console.log(code) }
随机生成9位数 (必须有字母大小写 必须有数字 的组合)
于 2022-08-24 11:11:49 首次发布