package com.utils;
import java.util.*;
public class RandomUtils {
private static final Random RANDOM = new Random();
private static final char[] NUMBERS = "0123456789".toCharArray();
private static final char[] HEXS = "0123456789abcdef".toCharArray();
private static final char[] LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
private static final char[] NUMBERS_AND_LETTERS = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
private static final char[] DELIMITER = ",.!?:;、。,!·:\n\t\r".toCharArray();
/**
* 产生length位的随机字符串
*
* @param length 字符长度
* @return 字符串
*/
public static String randomStr(int length) {
if (length < 1) {
throw new RuntimeException("length must be greater than 0");
}
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
randBuffer[i] = NUMBERS_AND_LETTERS[RANDOM.nextInt(72)];
}
return new String(randBuffer);
}
/**
* 产生length位的16进制
*
* @param length 字符长度
* @return 字符串
*/
public static String randomHex(int length) {
if (length < 1) {
throw new RuntimeException("length must be greater than 0");
}
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
randBuffer[i] = HEXS[RANDOM.nextInt(16)];
}
return new String(randBuffer);
}
/**
* 产生length长度的字母字符
*
* @param length 字符长度
* @return 字母字符串
*/
public static String randomLetter(int length) {
if (length < 1) {
throw new RuntimeException("length must be greater than 0");
}
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
randBuffer[i] = LETTERS[RANDOM.nextInt(51)];
}
return new String(randBuffer);
}
/**
* 产生length位的随机数字
*
* @param length 长度
* @return 数字字符串
*/
public static String randomNum(int length) {
if (length < 1) {
throw new RuntimeException("length must be greater than 0");
}
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
randBuffer[i] = NUMBERS[RANDOM.nextInt(10)];
if (i == 0) {
if (randBuffer[i] == '0') {
i--;
}
}
}
return new String(randBuffer);
}
/**
* 随即产生min到max之间的随机数,包含max和min
*
* @param min 最小值
* @param max 最大值
* @return 数字
*/
public static int randomInt(int min, int max) {
if (min == max) {
return min;
}
return RANDOM.nextInt(max + 1) % (max - min + 1) + min;
}
/**
* 随机产生min到max之间的long值
*
* @param min 最小值
* @param max 最大值
* @return 数字
*/
public static long randomLong(long min, long max) {
if(min == max){
return min;
}
return Math.abs(RANDOM.nextLong() % (max - min + 1)) + min;
}
public static double randomDouble(int length, int decimal) {
if (length <= decimal) {
return 0D;
}
return Double.parseDouble(randomNum(length - decimal) + "." + randomNum(decimal));
}
public static boolean randomBoolean() {
return RANDOM.nextInt(2) % 2 == 0;
}
public static String randomUUID() {
return UUID.randomUUID().toString();
}
/**
* 随机产生标点符号
*
* @return 标点符号
*/
public static char randomDelimiter() {
return DELIMITER[randomInt(0, DELIMITER.length - 1)];
}
public static String randomIp() {
return randomInt(1, 254) + "." + randomInt(1, 254) + "." + randomInt(1, 254) + "." + randomInt(1, 254);
}
/**
* 从数组中随机选择一个
* @param arr 数组
* @param <T> 泛型
* @return 对象
*/
public static <T> T randomChose(T... arr) {
return arr[randomInt(0, arr.length - 1)];
}
public static String randomPassword(int length) {
if (length < 8) {
throw new RuntimeException("password length must be greater than 8");
}
char[] lowerLetters = "abcdefghijklmnopqrstuvwxyz".toCharArray();
char[] upperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
List<String> password = new ArrayList<>();
// 2个数字
password.add(String.valueOf(NUMBERS[randomInt(0, NUMBERS.length - 1)]));
password.add(String.valueOf(NUMBERS[randomInt(0, NUMBERS.length - 1)]));
// 2个小写字母
password.add(String.valueOf(lowerLetters[randomInt(0, lowerLetters.length - 1)]));
password.add(String.valueOf(lowerLetters[randomInt(0, lowerLetters.length - 1)]));
// 2个大写字母
password.add(String.valueOf(upperLetters[randomInt(0, upperLetters.length - 1)]));
password.add(String.valueOf(upperLetters[randomInt(0, upperLetters.length - 1)]));
for (int i = 6; i < length - 1; i++) {
password.add(String.valueOf(NUMBERS_AND_LETTERS[randomInt(0, NUMBERS_AND_LETTERS.length - 1)]));
}
password.sort(Comparator.comparingInt(o -> randomInt(0, 10)));
return randomLetter(1) + CollectionUtils.join(password, "");
}
}
随机值工具类
最新推荐文章于 2024-04-06 15:14:39 发布