Open qappleh opened 3 years ago
/*
@param {Number} len 返回的 randomString 数组长度
@param {Number} strLen 随机验证码长度
*/
function randomString(len, strLen) {
let randomStr = []
for (let i=0; i<len; i++) {
setStr(randomStr, randomCode(strLen))
}
function setStr(arr, str) {
if (arr.includes(str)) {
setStr(arr, str)
} else {
arr.push(str)
return
}
}
function randomCode(strLen) {
let randomCode = Math.random().toString().split('.')[1].slice(0,6)
return randomCode
}
return randomStr
}
function randomString() {
let arr = [];
let map = {};
for (let i = 0; i < 1000; i++) {
let v = Math.random().toString().slice(2, 8);
while (map[v]) {
v = Math.random().toString().slice(2, 8);
}
map[v] = true;
arr.push(v);
}
return arr;
}
randomString() {
let arrSize = 0
let code = ''
const arrSet = new Set()
while (arrSize < 1000) {
code = Math.floor(Math.random() * 1000000) + ''
code.length === 6 ? arrSet.add(code) : code
arrSet.size === arrSize ? arrSize : arrSize++
}
return [...arrSet]
},