随机数工具类
import java.util.Random; public class RandomUtil { private static final String BASE_NUMBER = "0123456789"; private static final String BASE_CHAR = "abcdefghijklmnopqrstuvwxyz"; private static final String BASE_CHAR_NUMBER = "abcdefghijklmnopqrstuvwxyz0123456789"; private RandomUtil() { } /** * 获取范围内的随机数字 * @param min 最小值 * @param max 最大值 * @return */ public static int randomInt(int min, int max) { Random random = new Random(); return random.nextInt(max - min) + min; } /** * 获取大写的随机字符串 * @param length 长度 * @return */ public static String randomUcString(int length) { return randomString(BASE_CHAR_NUMBER, length).toUpperCase(); } /** * 获取小写的随机字符串 * @param length 长度 * @return */ public static String randomLcString(int length){ return randomString(BASE_CHAR_NUMBER, length); } /** * 获取随机数字字符串 * @param length 长度 * @return */ public static String randomNumbers(int length) { return randomString(BASE_NUMBER, length); } /** * 获取随机字符串 * @param baseString 字符串模板 * @param length 长度 * @return */ public static String randomString(String baseString, int length) { Random random = new Random(); StringBuilder sb = new StringBuilder(); if (length < 1) { length = 1; } int baseLength = baseString.length(); for(int i = 0; i < length; ++i) { int number = random.nextInt(baseLength); sb.append(baseString.charAt(number)); } return sb.toString(); } }