源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JAVA编程实现随机生成指定长度的密码功能【大小写和数字组合】

  • 时间:2022-12-25 01:45 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JAVA编程实现随机生成指定长度的密码功能【大小写和数字组合】
本文实例讲述了JAVA编程实现随机生成指定长度的密码功能。分享给大家供大家参考,具体如下:
import java.util.Random;
public class PassWordCreate {
  /**
   * 获得密码
   * @param len 密码长度
   * @return
   */
  public String createPassWord(int len){
    int random = this.createRandomInt();
    return this.createPassWord(random, len);
  }
  public String createPassWord(int random,int len){
    Random rd = new Random(random);
    final int maxNum = 62;
    StringBuffer sb = new StringBuffer();
    int rdGet;//取得随机数
    char[] str = { '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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    int count=0;
    while(count < len){
      rdGet = Math.abs(rd.nextInt(maxNum));//生成的数最大为62-1
      if (rdGet >= 0 && rdGet < str.length) {
        sb.append(str[rdGet]);
        count ++;
      }
    }
    return sb.toString();
  }
  public int createRandomInt(){
    //得到0.0到1.0之间的数字,并扩大100000倍
    double temp = Math.random()*100000;
    //如果数据等于100000,则减少1
    if(temp>=100000){
      temp = 99999;
    }
    int tempint = (int)Math.ceil(temp);
    return tempint;
  }
  public static void main(String[] args){
    PassWordCreate pwc = new PassWordCreate();
    System.out.println(pwc.createPassWord(8));
  }
}

[b]PS:这里再为大家提供两款功能类似的在线工具供大家参考:[/b] [b]在线随机数字/字符串生成工具: [/b][url=http://tools.jb51.net/aideddesign/suijishu]http://tools.jb51.net/aideddesign/suijishu[/url] [b]高强度密码生成器: [/b][url=http://tools.jb51.net/password/CreateStrongPassword]http://tools.jb51.net/password/CreateStrongPassword[/url] 更多关于java算法相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/632.htm]Java数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/947.htm]Java字符与字符串操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/830.htm]Java操作DOM节点技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/687.htm]Java文件与目录操作技巧汇总[/url]》和《[url=http://www.1sucai.cn/Special/682.htm]Java缓存操作技巧汇总[/url]》 希望本文所述对大家java程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部