Skip to content

Instantly share code, notes, and snippets.

@ixth
Created May 20, 2013 13:33
Show Gist options
  • Save ixth/5612252 to your computer and use it in GitHub Desktop.
Save ixth/5612252 to your computer and use it in GitHub Desktop.
Random alphanumeric string generation
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