JavaScript生成随机密码

生成一个长度为N位的随机密码

方法一:

function generateRandomPassword(length) {
  const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  let password = '';
  
  for (let i = 0; i < length; i++) {
    const randomIndex = Math.floor(Math.random() * charset.length);
    password += charset[randomIndex];
  }
  
  return password;
}

const randomPassword = generateRandomPassword(10); // 生成一个10位的随机密码
console.log(randomPassword); // 打印随机生成的密码

方法二:

function generateRandomPassword(length) {
  const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  let password = '';

  for (let i = 0; i < length; i++) {
    const charType = Math.floor(Math.random() * 3); // 0: 数字, 1: 大写字母, 2: 小写字母
    let randomChar = '';

    if (charType === 0) {
      randomChar = String.fromCharCode(Math.floor(Math.random() * 10) + 48); // 48-57 是数字的 Unicode 范围
    } else if (charType === 1) {
      randomChar = String.fromCharCode(Math.floor(Math.random() * 26) + 65); // 65-90 是大写字母的 Unicode 范围
    } else if (charType === 2) {
      randomChar = String.fromCharCode(Math.floor(Math.random() * 26) + 97); // 97-122 是小写字母的 Unicode 范围
    }

    password += randomChar;
  }

  return password;
}

const randomPassword = generateRandomPassword(10); // 生成一个10位的随机密码
console.log(randomPassword); // 打印随机生成的密码

方法三:

function getRandom(min, max) {
    return Math.round(Math.random() * (max - min) + min);
}

function getCode(length) {
    let code = '';
    for (var i = 0; i < length; i++) {
        var type = getRandom(1, 3);
        switch (type) {
            case 1:
                code += String.fromCharCode(getRandom(48, 57));//数字
                break;
            case 2:
                code += String.fromCharCode(getRandom(97, 122));//小写字母
                break;
            case 3:
                code += String.fromCharCode(getRandom(65, 90));//大写字母
                break;
        }
    }
    return code;
}

var randomPassword = getCode(15);

根据自己喜欢的风格,选其一就好

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值