書寫一個函數(shù),產(chǎn)生一個指定長度的隨機(jī)字符串教沾,字符串中只能包含大寫字母、小寫字母、數(shù)字
function getRandom(min,max){
// Math.random() 0~1之間的小數(shù)
// Math.random()*5 0~5之間的小數(shù)
// Math.random()*5+(3) (0+3)~(5+3)之間的小數(shù) 3最小值 8最大值
// Math.random()*10 0~10之間的小數(shù)
// Math.random()*10+(5) (0+5)~(10+5)之間的小數(shù) 5最小值 15最大值
// 從而得出 Math.random()*(max-min)+min
// // Math.floor向下取整
return Math.floor( Math.random()*(max-min)+min )
}
function randomStr(len){
let contentScope = "";
//將 Unicode 編碼轉(zhuǎn)為一個字符: String.fromCharCode();
// 大寫字母對應(yīng)的 Unicode
for(let i=65;i<65+26;i++){
contentScope+=String.fromCharCode( getRandom(65,65+26) )
}
// 小寫字母對應(yīng)的 Unicode
for(let i=97;i<97+26;i++){
contentScope+=String.fromCharCode( getRandom(97,97+26) )
}
// 叔子對應(yīng)的 Unicode
for(let i=48;i<48+10;i++){
contentScope+=String.fromCharCode( getRandom(48,48+10) )
}
let rmStr = "";
for(let i=0;i<len;i++){
let rm = getRandom(0,contentScope.length-1);
rmStr+=contentScope[rm]
}
console.log(rmStr)
return rmStr;
}
randomStr(10);