Java生成包含大写字母,小写字母以及数字的随机密码

您所在的位置:网站首页 大写小写数字密码的简单组合 Java生成包含大写字母,小写字母以及数字的随机密码

Java生成包含大写字母,小写字母以及数字的随机密码

2024-02-16 18:47| 来源: 网络整理| 查看: 265

转自 https://www.cnblogs.com/dongliyang/archive/2013/04/01/2994554.html

具体原理是先保证有数字大小写字母,再随机生成剩下字符,再将所有字符随机交换位置,至于重不重复,我算了下至多76T数据后

代码如下:

import java.util.Random; public final class PasswordUtils { private static Random random; private static long seed; static { seed = System.currentTimeMillis(); random = new Random(seed); } private static int uniform(int N){ return random.nextInt(N); } private static int uniform(int a,int b){ return a + uniform(b - a); } public static String getGeneratePassword(){ int len = 6; char[] chArr = new char[len]; chArr[0] = (char)('0' + uniform(0,10)); chArr[1] = (char)('A' + uniform(0,26)); chArr[2] = (char)('a' + uniform(0,26)); char[] codes = { '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(int i = 3; i < len; i++){ chArr[i] = codes[uniform(0,codes.length)]; } for(int i = 0; i < len; i++){ int r = i + uniform(len - i); char temp = chArr[i]; chArr[i] = chArr[r]; chArr[r] = temp; } return new String(chArr); } public static void main(String[] args) { for (int i = 0; i < 10000; i++) { System.out.println(getGeneratePassword()); } } }

 

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3