JAVA获取随即密码
- package cn.test;
- import java.util.HashSet;
- import java.util.Random;
- import java.util.Set;
- public class RandomKey {
-
- public static String genRandomNum(int pwd_len) {
- int i;
- int count = 0;
- 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', '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', };
- final int maxNum = str.length -
1;
- StringBuilder pwd = new StringBuilder("");
- Random r = new Random();
- while (count < pwd_len) {
- i = Math.abs(r.nextInt(maxNum));
- if (i >= 0 && i < str.length) {
- pwd.append(str[i]);
- count++;
- }
- }
- return pwd.toString();
- }
-
- public static
void main(String[] args) {
- Set<String> set = new HashSet<String>();
- String s = "";
- for(int i=0;i <
1000000;i++){
- s = genRandomNum(8);
- set.add(s);
- }
- System.out.println(set.size());
- }
- }
原文地址:https://www.cnblogs.com/bjanzhuo/p/3575923.html