生成100万条不重复的4位数字或字母(比如X0Au)
1:要求至少有一位小写字母
2:要求至少有一位大写字母
3:必须是【大写字母】开头
4:不允许出现【一位大写三位小写】或者【一位小写三位大写】(比如Abcd/ABCd等)
5 : 4位必须不同(大小写不敏感,比如X0Aa,只包含x,0,a三位,所以不允许)
6 : 具备不可枚举性
private void calculate() {
String[] charactor = new String[]{"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"};
boolean[] select = new boolean[62];
int randomInt;
int hasLowerCount = 0;
int hasUpperCount = 0;
StringBuilder resultBuilder = new StringBuilder();
for (int m = 0; m < 1000000; m++) {
for (int i = 0; i < 4; i++) {
if (i == 0) {
randomInt = (int) (Math.random() * 26);
} else {
randomInt = (int) (Math.random() * 62);
while (select[randomInt]
|| (randomInt + 26 < 52 && select[randomInt + 26])
|| (randomInt - 26 >= 0 && select[randomInt - 26])
|| (hasLowerCount > 1 && randomInt > 25 && randomInt < 52)
|| (hasUpperCount > 1 && randomInt < 26)) {
randomInt = (int) (Math.random() * 62);
}
}
if (randomInt < 26) {
hasUpperCount++;
}
if (randomInt >= 26 && randomInt < 52) {
hasLowerCount++;
}
select[randomInt] = true;
}
resultBuilder.append(m+1).append(",");
for (int i = 0; i < 62; i++) {
if (select[i]) {
select[i] = false;
resultBuilder.append(charactor[i]);
}
}
hasLowerCount = 0;
hasUpperCount = 0;
resultBuilder.append("\n");
}
writeToTxt(resultBuilder.toString());
}
private void writeToTxt(String result) {
File file = new File(getExternalCacheDir(), "T.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedWriter out = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream(file.getAbsoluteFile(),true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
out = new BufferedWriter(outputStreamWriter);
out.write(result);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
网友评论