package org.jimmy.autosearch2019.test;
import java.util.ArrayList;
import java.util.Random;
/**
* @author ラピスラズリ(Dawn)
* @date 2019年5月30日 下午3:18:48
* @detail 随机生成固定长度的字符串
*/
public class TestRandom2019053001 {
public static ArrayList<String> strList = new ArrayList<String>();
public static Random random = new Random();
public static final int RANDOM_LENGTH1 = 256;
public static final int RANDOM_LENGTH2 = 512;
public static final int RANDOM_LENGTH3 = 1024;
static {
init();
}
public static void main(String[] args) {
String randomStr = generateRandomStr(RANDOM_LENGTH1);
System.out.println(RANDOM_LENGTH1 + "位随机数:" + randomStr);
randomStr = generateRandomStr(RANDOM_LENGTH2);
System.out.println(RANDOM_LENGTH2 + "位随机数:" + randomStr);
randomStr = generateRandomStr(RANDOM_LENGTH3);
System.out.println(RANDOM_LENGTH3 + "位随机数:" + randomStr);
}
public static String generateRandomStr(int length) {
StringBuffer sb = new StringBuffer();
for(int i = 0; i < length; i++) {
int size = strList.size();
String randomStr = strList.get(random.nextInt(size));
sb.append(randomStr);
}
return sb.toString();
}
public static void init() {
int begin = 97;
//生成小写字母,并加入集合
for(int i = begin; i < begin + 26; i++) {
strList.add((char)i + "");
}
//生成大写字母,并加入集合
begin = 65;
for(int i = begin; i < begin + 26; i++) {
strList.add((char)i + "");
}
//将0-9的数字加入集合
for(int i = 0; i < 10; i++) {
strList.add(i + "");
}
}
}
运行后效果:
256位随机数:qZP66hBKD2qZUNNw0seH2auRjw7wvtVAUAlV6Ze5jZXzL8H6MXSKSKcA3a5ae116DhMLD0jXlbNQp1YlRpDIpijbn9EtqPlWsIbvlrE1fZtnY8K88N6uhOYsCZc44A064arq0bK4tkCvVcXLPTyGeHJkBfujOrQfpkydRGTY1cGVSUgdVhrtyDUjBpNf393WRygZKSDUy704B2hzXWw7lTRImD5Y1evtXmglo4xmJn7XogvlsnenKlNtnZhfIz42
512位随机数:z70aSppoq72xBjElVsedc3jVrscJKczb42kwgn1DNK3GoLKhmsIG0qme7APZB5kEXBer82Wz8DvGXQhLo2sZq1a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