From 55395ed327bca1af0e7f013821150b4ab8aa4b4a Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 17 Mar 2023 18:44:33 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=B7=B1=E5=AE=9E=E7=8E=B0=E9=9A=8F?= =?UTF-8?q?=E6=9C=BA=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E7=94=9F=E6=88=90?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xyz/zhouxy/plusone/util/RandomUtil.java | 18 +++++++++++++++ .../zhouxy/plusone/util/RandomUtilTests.java | 23 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/util/RandomUtil.java create mode 100644 plusone-basic/plusone-basic-common/src/test/java/xyz/zhouxy/plusone/util/RandomUtilTests.java diff --git a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/util/RandomUtil.java b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/util/RandomUtil.java new file mode 100644 index 0000000..df9253d --- /dev/null +++ b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/util/RandomUtil.java @@ -0,0 +1,18 @@ +package xyz.zhouxy.plusone.util; + +import java.util.concurrent.ThreadLocalRandom; + +public final class RandomUtil { + private RandomUtil() { + throw new IllegalStateException("Utility class"); + } + + public static String randomStr(char[] sourceCharacters, int length) { + ThreadLocalRandom random = ThreadLocalRandom.current(); + char[] result = new char[length]; + for (int i = 0; i < length; i++) { + result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)]; + } + return String.valueOf(result); + } +} diff --git a/plusone-basic/plusone-basic-common/src/test/java/xyz/zhouxy/plusone/util/RandomUtilTests.java b/plusone-basic/plusone-basic-common/src/test/java/xyz/zhouxy/plusone/util/RandomUtilTests.java new file mode 100644 index 0000000..ad3399b --- /dev/null +++ b/plusone-basic/plusone-basic-common/src/test/java/xyz/zhouxy/plusone/util/RandomUtilTests.java @@ -0,0 +1,23 @@ +package xyz.zhouxy.plusone.util; + +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +class RandomUtilTests { + + @Test + void testRandom() { + String[] s = new String[20]; + for (int i = 0; i < 20; i++) { + s[i] = RandomUtil.randomStr( + "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM~`!@#$%^&*()_-+={[\\|/:;\"',.<>?]}" + .toCharArray(), + 28); + } + log.info("{}", Arrays.toString(s)); + } +}