Created
May 20, 2013 13:33
-
-
Save ixth/5612252 to your computer and use it in GitHub Desktop.
Random alphanumeric string generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function randomChunk() { | |
return (Math.random() * Number.MAX_VALUE).toString(36); | |
} | |
function randomString(length) { | |
var result = ''; | |
do { | |
var chunk = randomChunk(); | |
var overhead = result.length + chunk.length - length; | |
if (overhead > 0) { | |
result += chunk.slice(0, chunk.length - overhead); | |
} else { | |
result += chunk; | |
} | |
} while (overhead < 0); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment