2023-07-18 15:29:07 +08:00
|
|
|
|
package xyz.zhouxy.plusone.commons.util;
|
|
|
|
|
|
2024-04-16 21:04:08 +08:00
|
|
|
|
import java.util.Random;
|
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
2023-07-18 15:29:07 +08:00
|
|
|
|
|
2024-04-03 16:20:41 +08:00
|
|
|
|
public final class RandomTools {
|
2024-04-16 21:04:08 +08:00
|
|
|
|
|
2024-04-03 16:20:41 +08:00
|
|
|
|
private RandomTools() {
|
2023-07-18 15:29:07 +08:00
|
|
|
|
throw new IllegalStateException("Utility class");
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 21:04:08 +08:00
|
|
|
|
public static String randomStr(char[] sourceCharacters, int length) {
|
|
|
|
|
return randomStr(ThreadLocalRandom.current(), sourceCharacters, length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 使用传入的随机数生成器,生成指定长度的字符串
|
|
|
|
|
*
|
|
|
|
|
* @param random 随机数生成器。根据需要可以传入
|
|
|
|
|
* {@link java.util.concurrent.ThreadLocalRandom}、{@link java.security.SecureRandom}
|
|
|
|
|
* 等,不为空
|
|
|
|
|
* @param sourceCharacters 字符池。字符串的字符将在数组中选,不为空
|
|
|
|
|
* @param length 字符串长度
|
|
|
|
|
* @return 随机字符串
|
|
|
|
|
*/
|
|
|
|
|
public static String randomStr(Random random, char[] sourceCharacters, int length) {
|
2023-07-18 15:29:07 +08:00
|
|
|
|
char[] result = new char[length];
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
|
result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)];
|
|
|
|
|
}
|
|
|
|
|
return String.valueOf(result);
|
|
|
|
|
}
|
|
|
|
|
}
|