重构。

dev
ZhouXY108 2024-04-16 21:04:08 +08:00
parent d2d760e866
commit b9d4901b67
1 changed files with 18 additions and 3 deletions

View File

@ -1,14 +1,29 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.security.SecureRandom; import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public final class RandomTools { public final class RandomTools {
private RandomTools() { private RandomTools() {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }
public static String secureRandomStr(char[] sourceCharacters, int length) { public static String randomStr(char[] sourceCharacters, int length) {
SecureRandom random = new SecureRandom(); 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) {
char[] result = new char[length]; char[] result = new char[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)]; result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)];