public class RandomUtil {
public static final int ANY_CHARACTER = 0;
public static final int ONLY_LOWER = 1;
public static final int ONLY_UPPER = 2;
public static final int ONLY_LOWER_UPPER = 3;
public static final int ONLY_NUMBER = 4;
private static final int DEFAULT_LENTH = 8;
private static int low = 1;
private static int high = 10;
private static final int BUFFER_SIZE = 101;
private static final int BUFFER_SIZE_CHAR = 72;
private static double[] buffer = new double[101];
private static char[] bufferChar = new char[72];
static {
for(int i = 0; i < 101; ++i) {
buffer[i] = Math.random();
}
initCharBuffer();
}
private static void initCharBuffer() {
String bufferString = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int length = bufferString.length();
for(int i = 0; i < length; ++i) {
bufferChar[i] = bufferString.charAt(i);
}
}
public RandomUtil() {
}
public RandomUtil(int low, int high) {
low = low;
high = high;
}
public static int nextIntRandom() {
int r = low + (int)((double)(high - low + 1) * nextRandom());
return r;
}
public static int nextIntRandom(int _low, int _high) {
low = _low;
high = _high;
return nextIntRandom();
}
private static double nextRandom() {
int pos = (int)(Math.random() * 101.0D);
if (pos == 101) {
pos = 100;
}
double value = buffer[pos];
buffer[pos] = Math.random();
return value;
}
public static String nextStringRandom(int length, int type) {
switch(type) {
case 1:
low = 36;
high = 61;
break;
case 2:
low = 10;
high = 35;
break;
case 3:
low = 10;
high = 61;
break;
case 4:
low = 0;
high = 9;
break;
default:
low = 0;
high = 61;
}
StringBuffer value = new StringBuffer();
for(int k = 0; k < length; ++k) {
int j = nextIntRandom();
value.append(bufferChar[j]);
}
return value.toString();
}
public static String nextStringRandom() {
return nextStringRandom(8, 5);
}
public static String getStringRandom(int length) {
String val = "";
Random random = new Random();
// 参数length,表示生成几位随机数
for (int i = 0; i < length; i++) {
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
// 输出字母还是数字
if ("char".equalsIgnoreCase(charOrNum)) {
// 输出是大写字母还是小写字母
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (random.nextInt(26) + temp);
} else if ("num".equalsIgnoreCase(charOrNum)) {
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
public static String getStringRandom(int length) {
String val = "";
java.util.Random random = new java.util.Random();
// 参数length,表示生成几位随机数
for (int i = 0; i < length; i++) {
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
// 输出字母还是数字
if ("char".equalsIgnoreCase(charOrNum)) {
// 输出是大写字母还是小写字母
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (random.nextInt(26) + temp);
} else if ("num".equalsIgnoreCase(charOrNum)) {
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
public static void main(String[] args) {
System.out.println("type0 == " + nextStringRandom(10, 0));
System.out.println("type1 == " + nextStringRandom(10, 1));
System.out.println("type2 == " + nextStringRandom(10, 2));
System.out.println("type3 == " + nextStringRandom(10, 3));
System.out.println("type4 == " + nextStringRandom(10, 4));
System.out.println("int == " + nextIntRandom());
System.out.println("int == " + nextIntRandom());
System.out.println("int == " + nextIntRandom(50,100));
System.out.println("int == " + nextRandom());
System.out.println("int == " + nextRandom());
}
}
最后提个工具类:cn.hutool.core.util.RandomUtil