132 lines
3.5 KiB
Java
132 lines
3.5 KiB
Java
package com.jinrui.assembly.utils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import java.util.Random;
|
|
import java.util.UUID;
|
|
|
|
/**
|
|
* @author gaowang
|
|
* @createTime 2020/7/3 14:51
|
|
*/
|
|
public final class RandomUtil {
|
|
private RandomUtil() {
|
|
}
|
|
|
|
/**
|
|
* 数字数组
|
|
*/
|
|
public final static String[] DIGIT_ARRAY = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
|
|
|
|
/**
|
|
* 字母数组
|
|
*/
|
|
public final static String[] LETTER_ARRAY = {"a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
|
|
"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y"};
|
|
|
|
/**
|
|
* 字母 数字 数组
|
|
*/
|
|
public static String[] MIX_ARRAY = new String[DIGIT_ARRAY.length + LETTER_ARRAY.length];
|
|
|
|
static {
|
|
System.arraycopy(DIGIT_ARRAY, 0, MIX_ARRAY, 0, DIGIT_ARRAY.length);
|
|
System.arraycopy(LETTER_ARRAY, 0, MIX_ARRAY, DIGIT_ARRAY.length, LETTER_ARRAY.length);
|
|
}
|
|
|
|
public static Random RANDOM = new Random();
|
|
|
|
/**
|
|
* 32 位 全球唯一码
|
|
*
|
|
* @return
|
|
*/
|
|
public static String GUID() {
|
|
return UUID.randomUUID().toString().replaceAll("-", "");
|
|
}
|
|
|
|
/**
|
|
* 36 位 全球唯一码
|
|
*
|
|
* @return
|
|
*/
|
|
public static String UUID() {
|
|
return UUID.randomUUID().toString();
|
|
}
|
|
|
|
/**
|
|
* 生成随机数字字符串
|
|
*
|
|
* @param length
|
|
* @return
|
|
*/
|
|
public static String randomDigit(int length) {
|
|
StringBuffer buffer = new StringBuffer();
|
|
for (int i = 0; i < length; i++) {
|
|
int index = RANDOM.nextInt(DIGIT_ARRAY.length);
|
|
if (i == 0) {
|
|
index = index + 1;
|
|
if (index == DIGIT_ARRAY.length) {
|
|
index--;
|
|
}
|
|
}
|
|
buffer.append(DIGIT_ARRAY[index]);
|
|
}
|
|
return buffer.toString();
|
|
}
|
|
|
|
/**
|
|
* 随机整数 耗时 randomInteger > randomDigit
|
|
*
|
|
* @param length 最大9
|
|
* @return
|
|
*/
|
|
public static int randomInteger(int length) {
|
|
int max = Integer.valueOf(StringUtils.rightPad("9", length > 9 ? 9 : length, "9"));
|
|
int min = Integer.valueOf(StringUtils.rightPad("1", length > 9 ? 9 : length, "0"));
|
|
int val = RANDOM.nextInt(max);
|
|
if (val < min) {
|
|
return val + min;
|
|
}
|
|
return val;
|
|
}
|
|
|
|
/**
|
|
* 生成随机字母字符串
|
|
*
|
|
* @param length
|
|
* @return
|
|
*/
|
|
public static String randomLetter(int length) {
|
|
StringBuffer buffer = new StringBuffer();
|
|
for (int i = 0; i < length; i++) {
|
|
int index = RANDOM.nextInt(LETTER_ARRAY.length);
|
|
buffer.append(LETTER_ARRAY[index]);
|
|
}
|
|
return buffer.toString();
|
|
}
|
|
|
|
/**
|
|
* 生成数字字母混合字符串
|
|
*
|
|
* @param length
|
|
* @return
|
|
*/
|
|
public static String randomMixChar(int length) {
|
|
StringBuffer buffer = new StringBuffer();
|
|
for (int i = 0; i < length; i++) {
|
|
int index = RANDOM.nextInt(MIX_ARRAY.length);
|
|
String indexChar = MIX_ARRAY[index];
|
|
if (RANDOM.nextInt(10) % 2 == 0) {
|
|
indexChar = indexChar.toUpperCase();
|
|
}
|
|
buffer.append(indexChar);
|
|
}
|
|
return buffer.toString();
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
System.out.println(randomMixChar(8));
|
|
}
|
|
}
|