Java生成随机密码

《Java编程的逻辑》

import java.util.Random;

public class RandomPwdV1 {

  /**
   * 生成随机密码:6位数字
   * 
   * @return 密码字符串
   */
  public static String randomPassword() {
    char[] chars = new char[6];
    Random rnd = new Random();
    for (int i = 0; i < 6; i++) {
      chars[i] = (char) ('0' + rnd.nextInt(10));
    }
    return new String(chars);
  }

  public static void main(String[] args) {
    System.out.println(randomPassword());
  }
}

import java.util.Random;

/**
 * 生成随机密码:简单8位
 * 对每个字符,先随机选类型,然后在给定类型中随机选字符
 */
public class RandomPwdV2 {
  private static final String SPECIAL_CHARS = "!@#3%&*_=+";

  private static char nextChar(Random rnd) {
    switch (rnd.nextInt(4)) {
      case 0:
        return (char) ('a' + rnd.nextInt(26));
      case 1:
        return (char) ('A' + rnd.nextInt(26));
      case 2:
        return (char) ('0' + rnd.nextInt(10));
      default:
        return SPECIAL_CHARS.charAt(rnd.nextInt(SPECIAL_CHARS.length()));
    }
  }

  /**
   * 生成随机密码:简单8位
   * 
   * @return
   */
  public static String randomPassword() {
    char[] chars = new char[8];
    Random rnd = new Random();
    for (int i = 0; i < 8; i++) {
      chars[i] = nextChar(rnd);
    }
    return new String(chars);
  }

  public static void main(String[] args) {
    System.out.println(randomPassword());
  }
}

import java.util.Random;

/**
 * 生成随机密码:复杂8位
 * nextIndex随机生成一个未赋值的位置,程序先随机生成4个不同类型的字符,放到随机位置上,然后给未赋值的其他位置随机生成字符。
 */
public class RandomPwdV3 {

  private static final String SPECIAL_CHARS = "!@#3%&*_=+";

  private static int nextIndex(char[] chars, Random rnd) {
    int index = rnd.nextInt(chars.length);
    while (chars[index] != 0) {
      index = rnd.nextInt(chars.length);
    }
    return index;
  }

  private static char nextSpecialChar(Random rnd) {
    return SPECIAL_CHARS.charAt(rnd.nextInt(SPECIAL_CHARS.length()));
  }

  private static char nextUpperlLetter(Random rnd) {
    return (char) ('A' + rnd.nextInt(26));
  }

  private static char nextLowerLetter(Random rnd) {
    return (char) ('a' + rnd.nextInt(26));
  }

  private static char nextNumLetter(Random rnd) {
    return (char) ('0' + rnd.nextInt(10));
  }

  private static char nextChar(Random rnd) {
    switch (rnd.nextInt(4)) {
      case 0:
        return (char) ('a' + rnd.nextInt(26));
      case 1:
        return (char) ('A' + rnd.nextInt(26));
      case 2:
        return (char) ('0' + rnd.nextInt(10));
      default:
        return SPECIAL_CHARS.charAt(rnd.nextInt(SPECIAL_CHARS.length()));
    }
  }

  /**
   * 生成随机密码:复杂8位
   * 
   * @return 密码字符串
   */
  public static String randomPassword() {
    char[] chars = new char[8];
    Random rnd = new Random();
    chars[nextIndex(chars, rnd)] = nextSpecialChar(rnd);
    chars[nextIndex(chars, rnd)] = nextUpperlLetter(rnd);
    chars[nextIndex(chars, rnd)] = nextLowerLetter(rnd);
    chars[nextIndex(chars, rnd)] = nextNumLetter(rnd);
    for (int i = 0; i < 8; i++) {
      if (chars[i] == 0) {
        chars[i] = nextChar(rnd);
      }
    }
    return new String(chars);
  }

  public static void main(String[] args) {
    System.out.println(randomPassword());
  }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值