The first method
The code copy is as follows:
/*
*@desc: Generate random strings
*@remark:toString method can accept a cardinality as a parameter, and this cardinality is capped from 2 to 36. If not specified, the default cardinality is decimal
*/
function generateRandomAlphaNum(len) {
var rdmString = "";
for (; rdmString.length < len; rdmString += Math.random().toString(36).substr(2));
return rdmString.substr(0, len);
}
The second method
The code copy is as follows:
//JS generates GUID functions, similar to NewID() in .net;
function S4() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function NewGuid() {
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4() + S4() + S4());
}
The third method
The code copy is as follows:
//JS generates GUID functions, similar to NewID() in .net;
function newGuid() {
var guid = "";
for (var i = 1; i <= 32; i++) {
var n = Math.floor(Math.random() * 16.0).toString(16);
guid += n;
if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
guid += "-";
}
return guid;
}
The fourth method
The code copy is as follows:
/*
*@desc: Generate random strings
*@demo:console.log(ranStr());
*/
;(function(){
//Number 0-9, uppercase letters, lowercase letters, ASCII or UNICODE encoding (decimal), a total of 62
var charCodeIndex = [[48,57],[65,90],[97,122]];
var charCodeArr = [];
function getBetweenRound(min,max){
return Math.floor(min+Math.random()*(max-min));
};
function getCharCode(){
for(var i=0,len=3;i<len;i++){
var thisArr = charCodeIndex[i];
for(var j=thisArr[0], thisLen=thisArr[1];j<=thisLen;j++){
charCodeArr.push(j);
}
}
}
function ranStr(slen){
slen = slen || 20;
charCodeArr.length<62 && getCharCode();
var res = [];
for(var i=0;i<slen;i++){
var index = getBetweenRound(0,61);
res.push(String.fromCharCode(charCodeArr[index]));
}
return res.join('');
};
this.ranStr = ranStr;
})();