Skip to content

Instantly share code, notes, and snippets.

@craigphicks
Created September 14, 2019 18:26
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 craigphicks/aa96696d303f20fb72f46f363c2f8b91 to your computer and use it in GitHub Desktop.
Save craigphicks/aa96696d303f20fb72f46f363c2f8b91 to your computer and use it in GitHub Desktop.
A non-crypto js function for creating random alphanum strings - no dependencies
function makeNonCryptoRandomAlphaNumString(length) {
function tf(n) {
if (n < 26) return 65 + n
else if (n < 52) return 97 + n - 26
else return 48 + n - 52
}
var result = ''
for (var i = 0; i < length; i++) {
let idx = Math.floor(Math.random() * 62)
result += String.fromCharCode(tf(idx))
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment