Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gilbarbara
Last active November 28, 2019 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilbarbara/559160eee86570fac61084be2af44f03 to your computer and use it in GitHub Desktop.
Save gilbarbara/559160eee86570fac61084be2af44f03 to your computer and use it in GitHub Desktop.
Generate a random alphanumeric string
function randomID(size = 6) {
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let text = '';
for (let i = 0; i < size; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
function uniqueID(size = 10) {
return [...Array(size)].map(() => (~~(Math.random() * 36)).toString(36)).join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment