Skip to content

Instantly share code, notes, and snippets.

@SeanJM
Last active March 14, 2017 12:58
Show Gist options
  • Save SeanJM/c44a8f3afb54104f5e082827cda9940c to your computer and use it in GitHub Desktop.
Save SeanJM/c44a8f3afb54104f5e082827cda9940c to your computer and use it in GitHub Desktop.
A function which generates a random id
(function () {
var lib = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var n = lib.length - 1;
function generateId(index) {
var id = [];
while (index-- > 0) {
id.push(lib[Math.round(Math.random() * n)]);
}
return id.join('');
}
if (typeof module === 'object') {
module.exports = generateId;
} else if (typeof window === 'object') {
window.generateId = generateId;
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment