Last active
May 19, 2016 09:09
-
-
Save genffy/2114e7aa3a90fdbfa0c0f9c3b525328e to your computer and use it in GitHub Desktop.
randomStr
This file contains 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 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