Skip to content

Instantly share code, notes, and snippets.

@hlfbt
Created September 16, 2018 17:31
Show Gist options
  • Save hlfbt/638105da1aa65ba5ab5a2f447181eed0 to your computer and use it in GitHub Desktop.
Save hlfbt/638105da1aa65ba5ab5a2f447181eed0 to your computer and use it in GitHub Desktop.
Generate random GUIDs usable as element IDs (first character is always a letter)
(function () {
var S4 = function (n, d) {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1) + (n > 1 ? (d || '') + S4(n - 1, d) : '');
};
return (String.fromCharCode(97 + Math.floor(Math.random() * 26)) + S4().substring(1) + S4(5, '-') + S4(2));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment