/ 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.
Expand |
Embed | Plain Text
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'], /*symbolChars: ['!', '@', '£', '$', '%', '^', '*', '(', ')', '_', '-', '~'],*/ generateUniqueID: function(uniqIDLength) { if(!uniqIDLength) var uniqIDLength = 10; var uniqueID = '', dateStamp = Date().toString().replace(/\s/g, ''); for(var uniqIDCounter = 0; uniqIDCounter < uniqIDLength; uniqIDCounter++) { uniqueID += this.alphaChars[Math.round(Math.random() * 25).toString()]; //uniqueID += this.symbolChars[Math.round(Math.random() * (this.symbolChars.length - 1)).toString()]; uniqueID += Math.round(Math.random() * 10); uniqueID += dateStamp.charAt(Math.random() * (dateStamp.length - 1)); } return uniqueID; }
You need to login to post a comment.