生成guid

JavaScript生成GUID的算法


全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) 。

GUID是一种由算法生成的二进制长度为128位的数字标识符。GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中的 x 是 0-9 或 a-f 范围内的一个32位十六进制数。在理想情况下,任何计算机和计算机集群都不会生成两个相同的GUID。

GUID 的总数达到了2^128(3.4×10^38)个,所以随机生成两个相同GUID的可能性非常小,但并不为0。GUID一词有时也专指微软对UUID标准的实现。

 

算法1

1
2
3
4
5
6
7
8
9
10
11
12
13
function  uuid() {
     var  s = [];
     var  hexDigits =  "0123456789abcdef" ;
     for  ( var  i = 0; i < 36; i++) {
         s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
     }
     s[14] =  "4" ;   // bits 12-15 of the time_hi_and_version field to 0010
     s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);   // bits 6-7 of the clock_seq_hi_and_reserved to 01
     s[8] = s[13] = s[18] = s[23] =  "-" ;
 
     var  uuid = s.join( "" );
     return  uuid;
}

 

算法2

1
2
3
4
5
6
function  guid() {
     return  'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' .replace(/[xy]/g,  function (c) {
         var  r = Math.random()*16|0, v = c ==  'x'  ? r : (r&0x3|0x8);
         return  v.toString(16);
     });
}

 

算法3

1
2
3
4
5
6
function  guid() {
     function  S4() {
        return  (((1+Math.random())*0x10000)|0).toString(16).substring(1);
     }
     return  (S4()+S4()+ "-" +S4()+ "-" +S4()+ "-" +S4()+ "-" +S4()+S4()+S4());
}

 

算法4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function  uuid(len, radix) {
     var  chars =  '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' .split( '' );
     var  uuid = [], i;
     radix = radix || chars.length;
 
     if  (len) {
       // Compact form
       for  (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
     else  {
       // rfc4122, version 4 form
       var  r;
 
       // rfc4122 requires these characters
       uuid[8] = uuid[13] = uuid[18] = uuid[23] =  '-' ;
       uuid[14] =  '4' ;
 
       // Fill in random data.  At i==19 set the high bits of clock sequence as
       // per rfc4122, sec. 4.1.5
       for  (i = 0; i < 36; i++) {
         if  (!uuid[i]) {
           r = 0 | Math.random()*16;
           uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
         }
       }
     }
 
     return  uuid.join( '' );
}

这个可以指定长度和基数。比如

1
2
3
4
5
6
// 8 character ID (base=2)
uuid(8, 2)   //  "01001010"
// 8 character ID (base=10)
uuid(8, 10)  // "47473046"
// 8 character ID (base=16)
uuid(8, 16)  // "098F4D35"

 

 

源自:

http://www.broofa.com/2008/09/javascript-uuid-function/

http://note19.com/2007/05/27/javascript-guid-generator/

http://www.ietf.org/rfc/rfc4122.txt

http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/873856#873856



  1. //表示全局唯一标识符 (GUID)。  
  2. function Guid(g) {  
  3.     var arr = new Array(); //存放32位数值的数组  
  4.     if (typeof (g) == "string") { //如果构造函数的参数为字符串  
  5.         InitByString(arr, g);  
  6.     }  
  7.     else {  
  8.         InitByOther(arr);  
  9.     }  
  10.   
  11.     //返回一个值,该值指示 Guid 的两个实例是否表示同一个值。  
  12.     this.Equals = function (o) {  
  13.         if (o && o.IsGuid) {  
  14.             return this.ToString() == o.ToString();  
  15.         }  
  16.         else {  
  17.             return false;  
  18.         }  
  19.     }  
  20.     //Guid对象的标记  
  21.     this.IsGuid = function () { }  
  22.     //返回 Guid 类的此实例值的 String 表示形式。  
  23.     this.ToString = function (format) {  
  24.         if (typeof (format) == "string") {  
  25.             if (format == "N" || format == "D" || format == "B" || format == "P") {  
  26.                 return ToStringWithFormat(arr, format);  
  27.             }  
  28.             else {  
  29.                 return ToStringWithFormat(arr, "D");  
  30.             }  
  31.         }  
  32.         else {  
  33.             return ToStringWithFormat(arr, "D");  
  34.         }  
  35.     }  
  36.     //由字符串加载  
  37.     function InitByString(arr, g) {  
  38.         g = g.replace(/\{| | |\}|-/g, "");  
  39.         g = g.toLowerCase();  
  40.         if (g.length != 32 || g.search(/[^0-9,a-f]/i) != -1) {  
  41.             InitByOther(arr);  
  42.         }  
  43.         else {  
  44.             for (var i = 0; i < g.length; i++) {  
  45.                 arr.push(g[i]);  
  46.             }  
  47.         }  
  48.     }  
  49.     //由其他类型加载  
  50.     function InitByOther(arr) {  
  51.         var i = 32;  
  52.         while (i--) {  
  53.             arr.push("0");  
  54.         }  
  55.     }  
  56.     /*  
  57.     根据所提供的格式说明符,返回此 Guid 实例值的 String 表示形式。  
  58.     N  32 位: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  
  59.     D  由连字符分隔的 32 位数字 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  
  60.     B  括在大括号中、由连字符分隔的 32 位数字:{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}  
  61.     P  括在圆括号中、由连字符分隔的 32 位数字:(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)  
  62.     */  
  63.     function ToStringWithFormat(arr, format) {  
  64.         switch (format) {  
  65.             case "N":  
  66.                 return arr.toString().replace(/,/g, "");  
  67.             case "D":  
  68.                 var str = arr.slice(0, 8) + "-" + arr.slice(8, 12) + "-" + arr.slice(12, 16) + "-" + arr.slice(16, 20) + "-" + arr.slice(20, 32);  
  69.                 str = str.replace(/,/g, "");  
  70.                 return str;  
  71.             case "B":  
  72.                 var str = ToStringWithFormat(arr, "D");  
  73.                 str = "{" + str + "}";  
  74.                 return str;  
  75.             case "P":  
  76.                 var str = ToStringWithFormat(arr, "D");  
  77.                 str = "(" + str + ")";  
  78.                 return str;  
  79.             default:  
  80.                 return new Guid();  
  81.         }  
  82.     }  
  83. }  
  84. //Guid 类的默认实例,其值保证均为零。  
  85. Guid.Empty = new Guid();  
  86. //初始化 Guid 类的一个新实例。  
  87. Guid.NewGuid = function () {  
  88.     var g = "";  
  89.     var i = 32;  
  90.     while (i--) {  
  91.         g += Math.floor(Math.random() * 16.0).toString(16);  
  92.     }  
  93.     return new Guid(g);  
  94. }  


实例:

[html]  view plain  copy
  1. var guid=Guid.NewGuid();  
  2. alert(guid.ToString());  



GUID(全球唯一标识)是微软使用的一个术语,由一个特定的算法,给某一个实体,如Word文档,创建一个唯一的标识,GUID值就是这个唯一的标识码.除了.Net有专门的方法生成外,JS也可以生成GUID,一般有两种方式,分别是

方法一:
//JS生成GUID函数,类似.net中的NewID();
function S4() 
{   
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);   
}    
function NewGuid() 
{   
   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());   
}
方法二:
function newGuid()
{
    var guid = "";
    for (var i = 1; i <= 32; i++){
      var n = Math.floor(Math.random()*16.0).toString(16);
      guid +=   n;
      if((i==8)||(i==12)||(i==16)||(i==20))
        guid += "-";
    }
    return guid;    
}

 

由于项目的需要,要在前台生成全球唯一标示,所以在这列出来,以便可以帮助到更多人!

http://hi.baidu.com/haofz1983/item/a9db11cb3113e2d597445249








使用方法:

1、  生成一个新GUID:var guid = Guid.NewGuid();

2、  生成一个所有值均为0的GUID:

a)         var guid = new Guid();

b)         var guid = Guid.Empty;

3、  比较两个GUID是否相等:g1.Equals(g2);

4、  获取Guid的字符串形式。其中, format为String类型的可选参数,其含义为:

a)         “N”: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

b)         “D”  由连字符分隔的 32 位数字 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

c)         “B”  括在大括号中、由连字符分隔的 32 位数字:{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

d)         “P”  括在圆括号中、由连字符分隔的 32 位数字:(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

 

代码如下:

//表示全局唯一标识符 (GUID)

function Guid(g){

     var arr = new Array(); //存放32位数值的数组

    

     if (typeof(g) == "string"){ //如果构造函数的参数为字符串

         InitByString(arr, g);

     }

     else{

         InitByOther(arr);

     }

     //返回一个值,该值指示 Guid 的两个实例是否表示同一个值。

     this.Equals = function(o){

         if (o && o.IsGuid){

              return this.ToString() == o.ToString();

         }

         else{

              return false;

         }

     }

     //Guid对象的标记

     this.IsGuid = function(){}

     //返回 Guid 类的此实例值的 String 表示形式。

     this.ToString = function(format){

         if(typeof(format) == "string"){

              if (format == "N" || format == "D" || format == "B" || format == "P"){

                   return ToStringWithFormat(arr, format);

              }

              else{

                   return ToStringWithFormat(arr, "D");

              }

         }

         else{

              return ToStringWithFormat(arr, "D");

         }

     }

     //由字符串加载

     function InitByString(arr, g){

         g = g.replace(//{|/(|/)|/}|-/g, "");

         g = g.toLowerCase();

         if (g.length != 32 || g.search(/[^0-9,a-f]/i) != -1){

              InitByOther(arr);

         }

         else{

              for (var i = 0; i < g.length; i++){

                   arr.push(g[i]);

              }

         }

     }

     //由其他类型加载

     function InitByOther(arr){

         var i = 32;

         while(i--){

              arr.push("0");

         }

     }

     /*

     根据所提供的格式说明符,返回此 Guid 实例值的 String 表示形式。

     N  32 位: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

     D  由连字符分隔的 32 位数字 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

     B  括在大括号中、由连字符分隔的 32 位数字:{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

     P  括在圆括号中、由连字符分隔的 32 位数字:(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

     */

     function ToStringWithFormat(arr, format){

         switch(format){

              case "N":

                   return arr.toString().replace(/,/g, "");

              case "D":

                   var str = arr.slice(0, 8) + "-" + arr.slice(8, 12) + "-" + arr.slice(12, 16) + "-" + arr.slice(16, 20) + "-" + arr.slice(20,32);

                   str = str.replace(/,/g, "");

                   return str;

              case "B":

                   var str = ToStringWithFormat(arr, "D");

                   str = "{" + str + "}";

                   return str;

              case "P":

                   var str = ToStringWithFormat(arr, "D");

                   str = "(" + str + ")";

                   return str;

              default:

                   return new Guid();

         }

     }

}

//Guid 类的默认实例,其值保证均为零。

Guid.Empty = new Guid();

//初始化 Guid 类的一个新实例。

Guid.NewGuid = function(){

     var g = "";

     var i = 32;

     while(i--){

         g += Math.floor(Math.random()*16.0).toString(16);

     }

     return new Guid(g);

}

 

本帖转自:http://www.cnblogs.com/sunnycoder/archive/2010/03/08/1681124.html





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值