JavaScript implementation generates GUIDs (globally uniform identifiers) _javascript tips

Source: Internet
Author: User
Tags repetition

A GUID (global uniform identifier) is a number that is generated on a single machine and is guaranteed to be unique to all machines in the same space-time. Typically, the platform provides an API to generate GUIDs. The generation algorithm is interesting, using the Ethernet card address, nanosecond time, chip ID code, and many possible numbers. The only drawback to GUIDs is that the resulting string is larger.

The GUID is in the form of: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

We all know that GUIDs are not very useful in front-end development, but if you need to insert an ID, and this ID corresponds to the backend and other operations that require GUIDs, we can generate a GUID for convenience.

Generally, in SQL, Java, C #, and other background or database language to generate a GUID is very simple, and the front-end does not directly generate a GUID method, you can only hand-write one. However, because the GUID needs to obtain the address of the Ethernet card, as well as the nanosecond time number. And the front-end access to this information is more difficult (know the child shoes please be sure to tell me), and we can simulate the implementation of the build GUID, the code is as follows:

* * Function: Generate a GUID code, the GUID is composed of 14 below date time and more than 18 16 random numbers, the GUID has a certain repetition probability, but the repetition probability is very low, in theory the repetition probability is 1/(16^18) per 10ms, i.e. 16 18 times 1. The probability of repetition is low to negligible * Disclaimer: This code is for the author to learn special, such as in the use of the user in the process due to the code problems caused by the loss, and the author does not have any relationship * Date: September 4, 2014 * Author: WYC/function GUID () {THIS.D

  ate = new Date (); /* To determine whether the initialization, if the following code is initialized, the following code will no longer execute, in practice only once/if (typeof this.newguid!= ' function ') {/* Generate GUID Code * * * * GUID.P
      Rototype.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 <) {Guidstr + = Math.floor (Math.random () *16). toString (16);
    Return This.formatguid (GUIDSTR); /* * Feature: Gets the GUID format of the current date, that is, a 8-digit date: 19700101 * Return value: Returns the GUID date formatNote string/GUID.prototype.getGUIDDate = function () {return this.date.getFullYear () + This.addzero (this.date.ge
    Tmonth () + 1) + This.addzero (This.date.getDay ()); /* * Feature: Gets the GUID format of the current time, that is, 8-digit time, including milliseconds, milliseconds is 2 digits: 12300933 * Return value: Returns the GUID of the date format of the string/GUID.prototype.getGUI DTime = function () {return This.addzero (this.date.getHours ()) + This.addzero (this.date.getMinutes ()) + This.addzero
    (This.date.getSeconds ()) + This.addzero (parseint (This.date.getMilliseconds ()/10)); /* * Features: Add 0 to a positive integer for one number, or a string that can be converted to a non-Nan number can also implement the * parameter: A parameter indicates that a number is ready to be added before 0 or a string that can be converted to a number * return value: If qualified, returns the  Note String type, otherwise return its own string/GUID.prototype.addZero = function (num) {if (num). toString ()!= ' NaN ' &&
      num >= 0 && num < {return ' 0 ' + math.floor (num);
      else {return num.tostring (); }/* * Features: Converts the value of the Y-system to the X-value parameter: The 1th argument represents the value to convert, and the 2nd parameter represents the system to convert, and the 3rd argument is optional, indicating the current number of numbers, or 10 * backBack value: Returns the converted string/GUID.prototype.hexadecimal = function (num, x, y) {if (y!= undefined) {return par
      Seint (Num.tostring (), y). toString (x);
      else {return parseint (num.tostring ()). toString (x); }/* * Feature: Format 32-bit string as a GUID mode string * parameter: The 1th parameter represents a 32-bit string * return value: A string in standard GUID format/Guid.prototyp
        E.formatguid = function (guidstr) {var str1 = guidstr.slice (0, 8) + '-', str2 = Guidstr.slice (8, 12) + '-',
      STR3 = Guidstr.slice + '-', STR4 = Guidstr.slice (+) + '-', STR5 = Guidstr.slice (20);
    Return str1 + str2 + str3 + STR4 + str5;

 }
  }
}

GUID Object

Just save it in a JS file and refer to it.

And then we just need.

Copy Code code as follows:

var guid = new GUID ();

Alert (Guid.NewGuid ());


The GUID code can be obtained.

The implementation principle is very simple, here only uses the system time and more than 18 hexadecimal random number composition, and uses the system time conversion to 16, thus although still may duplicate, but the repetition probability is extremely low, negligible.

The above for I write the method of generating GUIDs, if there is a better way to tell me, thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.