Java⽣成固定长度的随机字符串(以⼤⼩写字母和数字)
package org.st;
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 = (Int(size));
sb.append(randomStr);
}
String();
}
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位随机
数:qZP66hBKD2qZUNNw0seH2auRjw7wvtVAUAlV6Ze5jZXzL8H6MXSKSKcA3a5ae116DhMLD0jXlbNQp1YlRpDIpijbn9EtqPlWsIbvlrE1fZtnY8K88N6uhOYsCZc44A064arq0bK4tkCvVcXLPTyGeHJkBfujOrQfpkydRGT 512位随机
数:z70aSppoq72xBjElVsedc3jVrscJKczb42kwgn1DNK3GoLKhmsIG0qme7APZB5kEXBer82Wz8DvGXQhLo2sZq1aCEKcviC0NUYBKxe0r6dTCrvrdMiIJfUv9O0idBEveM8ivLwbFmO1CO8nhNQftdBVFc1eaMwnHnM 1024位随机
数:xQWiJx91GDPeIJwnWH3chn0dayyRF8NlKhWf9gdMkVImLmgY4PzpVcNjMnT0htcc6vPxCD8YVYf8seTikKc7jXHxJj03rsMMA9W6GBlRtynSfdPR1sTZT67TM2JDilkYKBuZSSOvOsXWMrMnsFEcMnAJHj4N0nAY

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。