Generate random numbers with GUID (basically 0 repeatability)

Random numbers are used in both the front-end and the back-end. Whether it is from the verification code or various generated status codes, they are basically randomly generated, and some lottery algorithms are also processed by random numbers. , There are many ways to generate random numbers, but in fact, many of them are very repetitive. Today, I will briefly talk about how to generate random numbers. A method with low repeatability GUID ( Globally Unique Identifier ) ​​is an algorithm generated A numeric identifier with a binary length of 128 bits

The principle of non-repetition: The total number of GUIDs reaches 2^128 (3.4×10^38), so the possibility of randomly generating two identical GUIDs is very small, but not 0. Algorithms used to generate GUIDs usually incorporate non-random parameters (such as time) to ensure that this duplication does not occur.

Code without BB:

js for GUID:

function GUID() {
    this.date = new Date();

    /* Determine whether it has been initialized. If the following code has been initialized, the following code will no longer be executed, and only executed once in practice*/
    if (typeof this.newGUID != 'function') {
        
        /* Generate GUID code */
        GUID.prototype.newGUID = function() {
            this.date = new Date();
            var guidStr = '';
                sexadecimalDate = this.hexadecimal(this.getGUIDDate(), 16);
                sexadecimalTime = this.hexadecimal(this.getGUIDTime(), 16);
            for (var i = 0; i < 9; i++) {
                guidStr += Math.floor(Math.random()*16).toString(16);
            }
            guidStr + = sexadecimalDate;
            guidStr + = sexadecimalTime;
            while(guidStr.length < 32) {
                guidStr += Math.floor(Math.random()*16).toString(16);
            }
            return this.formatGUID(guidStr);
        }
        /*
         * Function: Get the GUID format of the current date, that is, an 8-digit date: 19700101
         * Return value: Returns a string of words in GUID date format
         */
        GUID.prototype.getGUIDDate = function() {
            return this.date.getFullYear() + this.addZero(this.date.getMonth() + 1) + this.addZero(this.date.getDay());
        }

        /*
         * Function: Get the GUID format of the current time, that is, the time of 8 digits, including the millisecond, and the millisecond is 2 digits: 12300933
         * Return value: Returns a string of words in GUID date format
         */
        GUID.prototype.getGUIDTime = function() {
            return this.addZero(this.date.getHours()) + this.addZero(this.date.getMinutes()) + this.addZero(this.date.getSeconds()) + this.addZero( parseInt(this.date.getMilliseconds() / 10 ));
        }

        /*
         * Function: Add 0 in front of a one-digit positive integer, if it is a string that can be converted to a non-NaN number, it can also be implemented
         * Parameter: The parameter represents a number to be added with 0 in front or a string that can be converted into a number
         * Return value: If the condition is met, return the type of the word string after adding 0, otherwise return its own string
         */
        GUID.prototype.addZero = function(num) {
            if (Number(num).toString() != 'NaN' && num >= 0 && num < 10) {
                return '0' + Math.floor(num);
            } else {
                return num.toString();
            }
        }

        /*
         * Function: Convert the y-base value to the x-base value
         * Parameters: The first parameter indicates the value to be converted; the second parameter indicates the base to be converted; the third parameter is optional, indicating the current base number, if not written, it is 10
         * Return value: return the converted string
         */
        GUID.prototype.hexadecimal = function(num, x, y) {
            if (y != undefined) {
                return parseInt(num.toString(), y).toString(x);
            } else {
                return parseInt(num.toString()).toString(x);
            }
        }
        /*
         * Function: Format 32-bit strings as GUID-mode strings
         * Parameters: The first parameter represents a 32-bit string
         * Return value: String in standard GUID format
         */
        GUID.prototype.formatGUID = function(guidStr) {
            var str1 = guidStr.slice (0, 8) + '-',
                str2 = guidStr.slice(8, 12) + '-',
                str3 =  guidStr.slice(12, 16) + '-',
                str4 = guidStr.slice(16, 20) + '-',
                str5 = guidStr.slice(20);
            return str1 + str2 + str3 + str4 + str5;
        }
    }
}

HTML code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="../js/guid.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		   
	</body>
	<script type="text/javascript">
		 var guid = new GUID ();
		 var radom = guid.newGUID ();
		 /*Regular processing, remove the middle -*/
		 var str = radom.replace(/-/g, "");
		 alert (work);
		 alert(str);
	</script>
</html>

Show results:


Remove the middle - later effect:





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325772045&siteId=291194637