JavaScript生成一个 带有随机 大小写字母 数字的固定长度字符串

本身不是很复杂 那就直接看代码吧

function randomWord(range){
    
    
    var str = "",
    arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    for(var i=0; i<range; i++){
    
    
        let pos = Math.round(Math.random() * (arr.length-1));
        str += arr[pos];
    }
    return str;
}
console.log(randomWord(43))

我这里是随机 生成了43个 当然 你们可以根据自己的需要去改

猜你喜欢

转载自blog.csdn.net/weixin_45966674/article/details/123554004