随机生成密码(包含数字、字符、字母)

201 阅读1分钟

getPassword( min, max){

        //min 最小位数 max最大限制
        
        let str = ''
        let range = (max?Math.round(Math.random()*(max-min))+min:min)
        let 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++) {
                var index = Math.round(Math.random() * (arr.length - 1))
            
            str += arr[index];
            
        }
        return str;
    }
    
    
    this.getPassword(8,20)