分享一段非常强大的随机数生成功能代码实例,有需要的朋友可以做一下参考。
代码实例如下:
(function(){
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 index=0,len=3;index<len;index++){
var thisArr = charCodeIndex[index];
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;
})();
console.log(this.ranStr(4));
web前端开发学习扣qun:731771211 详细教程分享