Skip to content

Instantly share code, notes, and snippets.

@genffy
Last active May 19, 2016 09:09
Show Gist options
  • Save genffy/2114e7aa3a90fdbfa0c0f9c3b525328e to your computer and use it in GitHub Desktop.
Save genffy/2114e7aa3a90fdbfa0c0f9c3b525328e to your computer and use it in GitHub Desktop.
randomStr
function randomStr(len) {
var str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
strLen = str.length,
arr = [];
while (len > 0) {
var index = Math.random() * strLen,
a = str.slice(index, index + 1);
if (a) {
arr.push(a);
len--;
}
}
return arr.join('');
}
console.log(randomStr(10));
console.log(randomStr(20));
console.log(randomStr(30));
console.log(randomStr(100));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment