-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* 实现指定个数的随机生成[a-zA-Z],[0-9],或者自定义是字符串 | ||
**/ | ||
// 不加自定义字符 | ||
function randomStr(len) { | ||
var ret = ''; | ||
var randomChar = function () { | ||
var str = Math.floor(Math.random() * 62); | ||
if (str < 10) { | ||
return str; // 0-9 | ||
} else if (str < 36) { | ||
return String.fromCharCode(str + 55); //A-Z | ||
} else { | ||
return String.fromCharCode(str + 61); // a-z | ||
} | ||
} | ||
while (ret.length < len) { | ||
ret += randomChar(); | ||
} | ||
return ret; | ||
} | ||
|
||
//加上自定义字符 | ||
function randomStr(len, charSet) { | ||
var ret = ''; | ||
var charSetLen = 62 + charSet.length; | ||
var randomChar = function () { | ||
var str = Math.floor(Math.random() * charSetLen); | ||
if (str < 10) { | ||
return str; // 0-9 | ||
} else if (str < 36) { | ||
return String.fromCharCode(str + 55); //A-Z | ||
} else if (str < 62) { | ||
return String.fromCharCode(str + 61); // a-z | ||
} else { | ||
return charSet[charSetLen - str - 1] | ||
} | ||
} | ||
while (ret.length < len) { | ||
ret += randomChar(); | ||
} | ||
return ret; | ||
} |