Don’t say much, just go to the code.
package org.bood.common.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 随机数据工具类
*
* @author bood
* @since 2024/01/05
*/
public class RandomUtils {
private static final char[] NUMBERS = "0123456789".toCharArray();
private static final char[] CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
private static final char[] ALPHANUMERIC = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
private static final char[] SPECIAL_CHARACTERS = (
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=/" +
"+-*_~!#@%^&()|.`,:"
).toCharArray();
private static final String[] TEL_PREFIXES = {
"130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
"150", "151", "152", "153", "155", "156", "157", "158", "159", "177",
"181", "187", "189", "199"
};
private RandomUtils() {
}
/**
* 生成指定长度的随机数字字符串
*
* @param size 数字长度
* @return {@link String }
* @author bood
* @since 2024/01/05
*/
public static String getRandomNum(int size) {
StringBuilder sb = new StringBuilder(size);
Random rd = new Random();
for (int i = 0; i < size; i++) {
sb.append(NUMBERS[rd.nextInt(NUMBERS.length)]);
}
return sb.toString();
}
/**
* 生成指定长度的随机字母字符串(可含数字)
*
* @param size 字符串长度
* @param withNum 是否包含数字
* @return {@link String }
* @author bood
* @since 2024/01/05
*/
public static String getRandomStr(int size, boolean withNum) {
StringBuilder sb = new StringBuilder(size);
Random rd = new Random();
char[] source = withNum ? ALPHANUMERIC : CHARACTERS;
for (int i = 0; i < size; i++) {
sb.append(source[rd.nextInt(source.length)]);
}
return sb.toString();
}
/**
* 生成指定长度的随机字符串,包括特殊字符
*
* @param size 字符串长度
* @return {@link String }
* @author bood
* @since 2024/01/05
*/
public static String getRandomStrWithSpecialChar(int size) {
StringBuilder sb = new StringBuilder(size);
Random rd = new Random();
for (int i = 0; i < size; i++) {
sb.append(SPECIAL_CHARACTERS[rd.nextInt(SPECIAL_CHARACTERS.length)]);
}
return sb.toString();
}
/**
* 生成随机双精度浮点数
*
* @return double
* @author bood
* @since 2024/01/05
*/
public static double getRandomDouble() {
return Math.random();
}
/**
* 生成指定范围的随机整数
*
* @param min 最小值
* @param max 最大值(不包括)
* @return int
* @author bood
* @since 2024/01/05
*/
public static int getRandomInt(int min, int max) {
return new Random().nextInt(max - min) + min;
}
/**
* 生成随机手机号列表
*
* @param total 生成手机号的数量
* @return {@link List }<{@link String }>
* @author bood
* @since 2024/01/05
*/
public static List<String> getTel(int total) {
List<String> tels = new ArrayList<>();
for (int i = 0; i < total; i++) {
String prefix = TEL_PREFIXES[new Random().nextInt(TEL_PREFIXES.length)];
String suffix = String.format("%08d", getRandomInt(0, 100000000));
tels.add(prefix + suffix);
}
return tels;
}
/**
* 字符串部分加密,使用指定符号替换中间的字符
*
* @param str 需要加密的字符串
* @param symbol 替换的符号
* @param beforeLen 前面保留的字符数量
* @param afterLen 后面保留的字符数量
* @return {@link String }
* @author bood
* @since 2024/01/05
*/
public static String encryptStr(String str, String symbol, int beforeLen, int afterLen) {
int length = str.length();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
if (i < beforeLen || i >= (length - afterLen)) {
sb.append(str.charAt(i));
} else {
sb.append(symbol);
}
}
return sb.toString();
}
}