Posted By

jatkins on 11/17/12


Tagged

unique id guid


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

ericmourant


Generate unique ID


 / Published in: JavaScript
 

Not necessarily perfectly unique, but the chances of two identical IDs being generated are insanely remote (so not really a GUID generator, but looks like it). You'll need to clear it up -- I pulled it out of an object containing miscellaneous functions.

  1. alphaChars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'],
  2. /*symbolChars: ['!', '@', '£', '$', '%', '^', '*', '(', ')', '_', '-', '~'],*/
  3.  
  4. generateUniqueID: function(uniqIDLength) {
  5. if(!uniqIDLength)
  6. var uniqIDLength = 10;
  7.  
  8. var uniqueID = '', dateStamp = Date().toString().replace(/\s/g, '');
  9. for(var uniqIDCounter = 0; uniqIDCounter < uniqIDLength; uniqIDCounter++) {
  10. uniqueID += this.alphaChars[Math.round(Math.random() * 25).toString()];
  11. //uniqueID += this.symbolChars[Math.round(Math.random() * (this.symbolChars.length - 1)).toString()];
  12. uniqueID += Math.round(Math.random() * 10);
  13. uniqueID += dateStamp.charAt(Math.random() * (dateStamp.length - 1));
  14. }
  15.  
  16. return uniqueID;
  17. }

Report this snippet  

You need to login to post a comment.