Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate random string/characters in JavaScript
// dec2hex :: Integer -> String
function dec2hex (dec) {
return ('0' + dec.toString(16)).substr(-2);
}
// generateId :: Integer -> String
function generateId (len = 40) {
const arr = new Uint8Array(len / 2);
window.crypto.getRandomValues(arr);
return Array.from(arr, dec2hex).join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment