[JS] Imitate QQ account generation | generate user id

rule

  1. Control the number of occurrences of the same number to prevent the generation of fancy numbers, such as 8866666
  2. Controlling the generation of lowest and highest digits
  3. Control the number of generated

train of thought

  1. Randomly generate an array of n elements, e.g.[2,3,5,4,2,1,3,5,5,5,5]
  2. Check for duplicates in the generated array, and replace elements with a single number that repeats more than the specified number of times

js code

Parameter Description

parameter Defaults type effect
num 1 Number The number of ids generated
min 7 Number id minimum number of digits
max 11 Number id maximum number of digits
repeatCount 2 Number Repeating number of a single number. For example, the value is 2, which means that a number appears up to 2 times
return value null [Array,String] When the parameter num is 1, return String, otherwise return Array

createUserID.js

/**
 * 生成用户id 
 * @param {Number} num 生成id数量,默认 1
 * @param {Number} min id最小位数,默认 7
 * @param {Number} max id最大位数,默认 11
 * @param {Number} repeatCount 单个数字重复数,默认 2,意思就是一个数字最多出现2次
 * @returns {Array,String} 返回值。参数num为1时,返回String,否则返回Array
 */
const createUserID = (num = 1, min = 7, max = 11, repeatCount = 2) => {
    
    
    let userIDs = []

    // 生成num个id
    for (let i = 0; i < num; i++) {
    
    
        let createId = [], numrepeatInfo = []

        //  用户id长度 ,最小min ,最大max
        const userLen = Math.round(Math.random() * (max - min)) + min

        // 生成userLen个数组 
        for (let index = 0; index < userLen; index++) {
    
    
            // 生成1-9数字
            const idNum = Math.ceil(Math.random() * 9)
            createId.push(idNum)
        }

        // 1-9数字出现次数
        for (let i = 1; i < 10; i++) {
    
    
            const iObj = {
    
    }

            // i在createId出现的次数
            const iCount = createId.filter(it => it === i)

            iObj.val = i
            iObj.count = iCount.length

            numrepeatInfo.push(iObj)
        }

        // 数组查重 | 重复次数大于2的数字
        const userIdRepeatGtTwo = createId.reduce((repeatIdInfo, cv, ci, arr) => {
    
    
            // 是否有cv元素 
            const findEd = repeatIdInfo.findIndex(it => it.val === cv)
            if (findEd !== -1) return repeatIdInfo

            // 当前数组信息
            const cvInfo = {
    
    
                repeatNumIndex: [],
                val: cv
            }

            // cv重复的次数
            arr.forEach((it, iti) => it === cv ? cvInfo.repeatNumIndex.push(iti) : '')

            // Info.repeatNumIndex.length大于repeatCount
            if (cvInfo.repeatNumIndex.length > repeatCount) {
    
    
                // 替换repeatCount次 
                const replaceCount = cvInfo.repeatNumIndex.length - repeatCount
                cvInfo.replaceCount = replaceCount

                repeatIdInfo.push(cvInfo)
            }

            return repeatIdInfo
        }, [])


        // userIdRepeatGtTwo有长度
        if (userIdRepeatGtTwo.length) {
    
    
            userIdRepeatGtTwo.forEach(cv => {
    
    
                // 替换的次数
                for (let index = 0; index < cv.replaceCount; index++) {
    
    
                    const repMinCount = numrepeatInfo.sort((a, b) => a.count - b.count)[0].count // numrepeatInfo重复数最小的数字
                    const findMinCountArr = numrepeatInfo.filter(cur => cur.count === repMinCount)//与repMinCount最小重复数的相同的数
                    const minrdm = Math.floor(Math.random() * findMinCountArr.length)//随机最小重复数字的下标
                    const rdmRepNum = findMinCountArr[minrdm].val//替换的数字

                    // 替换的随机下标
                    const numRepIndex = Math.floor(Math.random() * cv.repeatNumIndex.length)

                    const repI = cv.repeatNumIndex[numRepIndex]
                    createId[repI] = rdmRepNum

                    numrepeatInfo[minrdm].count++
                }
            })
        }

        if (num === 1) {
    
    
            // 数组转字符
            userIDs = createId.toString().replace(/,/g, '')
        } else {
    
    
            const id = createId.toString().replace(/,/g, '')
            userIDs.push(id)
        }
    }

    return userIDs
}

console.log(createUserID())
console.log('-------------华丽的分割线-------------------')
console.log(createUserID(100))

operation result

insert image description here

Guess you like

Origin blog.csdn.net/qq_43614372/article/details/130898133