Forged form data part of form builder random vehicle license plate

A few days ago, I recorded the mongodb part of the form data storage structure of the form builder, and then I wanted to forge some data. Why forge data? It's a shame to say that the PC and mobile end of the drag and drop design form and the list corresponding to the form should display which fields and whether they support the search and printing (that is, the list configuration of the form) and a series of configurations have not been done. In addition, only the form related design has been mentioned in the previous section. After the design is completed, the corresponding fields (such as the input data) have not been done All in all, there is only data structure now, but I want to write a query, so I can only forge some data. Also think of these data as real as possible, so I want to write some code to forge data. When writing the code, I want to randomly generate a vehicle license plate, so I have this note.

I'm going. I've said too much. Hurry to the topic.

In fact, it's quite simple to get a random number and then generate a license plate in combination with the previous note: first, get a province at random, and then get six characters in numbers and letters at random, so you get a license plate. But later I thought, since it is randomly generated, there will be duplicate, and the newly generated license plate should be excluded from the existing license plate. Let's take a look at the first version and go straight to the code:

var getRangeRandomNumber = function(num1,num2){ 
    num1 = Number.isInteger(num1) ? num1: 0;
    num2 = Number.isInteger(num2) ? num2: 0;
    var minNum=Math.min(num1,num2),maxNum=Math.max(num1,num2);
    return Math.round(Math.random() * (maxNum - minNum)) + minNum;
}; 
var getRandomLicensePlate1=function(){
    var strProvinceShorter="Beijing";
    var strNumberLetter="12345";
    var tempRetLicensePlate=strProvinceShorter[getRangeRandomNumber(strProvinceShorter.length-1)];
    for(var i=0;i<5;i++){
        tempRetLicensePlate+=strNumberLetter[getRangeRandomNumber(strNumberLetter.length-1)];
    }
    return tempRetLicensePlate;
};
var tempLicensePlateObj={},licensePlateCounter=Math.pow(5,5);
for(var i=1;i<=licensePlateCounter;i++){
    var temp = getRandomLicensePlate1();
    if(tempLicensePlateObj[temp]){
        tempLicensePlateObj[temp]++;
    }
    else{
        tempLicensePlateObj[temp]=1;
    }
}
console.log("Want to get"+licensePlateCounter+"Different license plates");
console.log("The result is"+Object.getOwnPropertyNames(tempLicensePlateObj).length+"Different license plates");
console.log("*******************************Here is the license plate details generated*******************************");
console.log(JSON.stringify(tempLicensePlateObj));
console.log("*******************************Details of multiple occurrences of the same license plate: start*******************************");
for (var key in tempLicensePlateObj) {
    if (tempLicensePlateObj.hasOwnProperty(key)) {
        if(tempLicensePlateObj[key]>1){
            console.log("License plate No.:["+key+"]Generated"+tempLicensePlateObj[key]+"second");
        }
    }
}
console.log("*******************************Multiple details of the same license plate: end*******************************");

In order to generate a high probability of duplicate license plates, the first test here narrows the value range:

Looking at the test results, the repetition rate is still very high, so how to generate a non repeated license plate? I think of a stupid method: pass the existing vehicle license plate as a parameter to the method to obtain the vehicle license plate, and make a judgment before returning. If it is included in the array, get it recursively until a non repetitive one is obtained. Here is the code of test 2:

var getRangeRandomNumber = function(num1,num2){ 
    num1 = Number.isInteger(num1) ? num1: 0;
    num2 = Number.isInteger(num2) ? num2: 0;
    var minNum=Math.min(num1,num2),maxNum=Math.max(num1,num2);
    return Math.round(Math.random() * (maxNum - minNum)) + minNum;
}; 
// Get random license plate
var getRandomLicensePlate2=(function f(excludeArr){
    if(!Array.isArray(excludeArr))excludeArr=[];
    //Logic of obtaining license plate
    var strProvinceShorter="Beijing";
    var strNumberLetter="12345";
    var tempRetLicensePlate=strProvinceShorter[getRangeRandomNumber(strProvinceShorter.length-1)];
    for(var i=0;i<5;i++){
        tempRetLicensePlate+=strNumberLetter[getRangeRandomNumber(strNumberLetter.length-1)];
    }
    if(excludeArr.indexOf(tempRetLicensePlate)>=0){
        tempRetLicensePlate = f(excludeArr);
    } else {
        excludeArr.push(tempRetLicensePlate);
    }
    return tempRetLicensePlate;
});

var tempLicensePlateObj={},licensePlateCounter=Math.pow(5,5),tempAllLicensePlates=[];
for(var i=1;i<=licensePlateCounter;i++){
    var temp = getRandomLicensePlate2(tempAllLicensePlates);
    if(tempLicensePlateObj[temp]){
        tempLicensePlateObj[temp]++;
    }
    else{
        tempLicensePlateObj[temp]=1;
    }
}
console.log("Want to get"+licensePlateCounter+"Different license plates");
console.log("The result is"+Object.getOwnPropertyNames(tempLicensePlateObj).length+"Different license plates");
console.log("*******************************Here is the license plate details generated*******************************");
console.log(JSON.stringify(tempLicensePlateObj));
console.log("*******************************Details of multiple occurrences of the same license plate: start*******************************");
for (var key in tempLicensePlateObj) {
    if (tempLicensePlateObj.hasOwnProperty(key)) {
        if(tempLicensePlateObj[key]>1){
            console.log("License plate No.:["+key+"]Generated"+tempLicensePlateObj[key]+"second");
        }
    }
}
console.log("*******************************Multiple details of the same license plate: end*******************************");

It turns out that the stack overflowed I doubt my recursion. Let's see the next test result

This time, the number obtained (the maximum value minus 5) is 5 times less than that of stack overflow, so no error is reported. Next, I narrow the value range and change it to four digits to see the result

There is no problem in this test. If there is no problem in the last two tests, it means that the recursion I wrote has no problem Is it really because the memory can not be installed??? Who knows why? Or there is a better way to get the license plate. Please give me some advice. Thank you

Here is the final code:

var getRangeRandomNumber = function(num1,num2){ 
    num1 = Number.isInteger(num1) ? num1: 0;
    num2 = Number.isInteger(num2) ? num2: 0;
    var minNum=Math.min(num1,num2),maxNum=Math.max(num1,num2);
    return Math.round(Math.random() * (maxNum - minNum)) + minNum;
}; 
var getRandomLicensePlate=(function f(excludeArr){
    if(!Array.isArray(excludeArr))excludeArr=[];
    //    Generate a random license plate
    var strProvinceShorter="Beijing, Tianjin, Hebei, Jinmeng, Liaoji, Heihu, Jiangsu, Zhejiang, Anhui, Fujian, Jiangxi, Shandong, Henan, Hunan, Guangdong, Qiongyu, Sichuan, Guizhou, Yunnan, Tibet, Shaanxi, Gansu, Qingning, new Hong Kong, Macao and Taiwan";
    var strNumberLetter="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var tempRetLicensePlate=strProvinceShorter[getRangeRandomNumber(strProvinceShorter.length-1)];
    for(var i=0;i<6;i++){
        tempRetLicensePlate+=strNumberLetter[getRangeRandomNumber(strNumberLetter.length-1)];
    }
    //    Judge whether the license plate exists
    if(excludeArr.indexOf(tempRetLicensePlate)>=0){
        tempRetLicensePlate = f(excludeArr);
    } else {
        excludeArr.push(tempRetLicensePlate);
    }
    return tempRetLicensePlate;
});

Finally, I used this test again to generate 100000 pieces without any problem:

After another test, 1000000 of them are generated and directly stuck Come on, let's get here!

Tags: Javascript JSON MongoDB Mobile less

Posted on Wed, 06 Nov 2019 23:42:34 -0500 by bow-viper1