Class OpenRandom

java.lang.Object
cloud.opencode.base.core.random.OpenRandom

public final class OpenRandom extends Object
Random Utility Class - Comprehensive random generation utilities 随机工具类 - 全面的随机生成工具

Provides random number, string, UUID generation with ThreadLocalRandom and SecureRandom.

提供基于 ThreadLocalRandom 和 SecureRandom 的随机数、字符串、UUID 生成。

Features | 主要功能:

  • Random numbers (int, long, double, boolean) - 随机数生成
  • Random strings (alphanumeric, numeric, alphabetic) - 随机字符串
  • Secure random (cryptographically strong) - 安全随机数
  • UUID generation (standard and simple) - UUID 生成
  • Collection random (element selection, shuffle) - 集合随机操作

Usage Examples | 使用示例:

int num = OpenRandom.randomInt(100);
String code = OpenRandom.randomAlphanumeric(8);
String uuid = OpenRandom.simpleUUID();
String element = OpenRandom.randomElement(list);
OpenRandom.shuffle(array);

Security | 安全性:

  • Thread-safe: Yes (ThreadLocalRandom) - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    randomAlphabetic(int length)
    Generates a random alphabetic string 生成随机字母字符串
    static String
    randomAlphanumeric(int length)
    Generates a random alphanumeric string 生成随机字母数字字符串
    static boolean
    Generates a random boolean 生成随机布尔值
    static byte[]
    randomBytes(int length)
    Generates a random byte array 生成随机字节数组
    static LocalDate
    randomDate(int startYear, int endYear)
    Generates a random date [startYear, endYear) 生成随机日期 [startYear, endYear)
    static LocalDate
    Generates a random date (specified range) 生成随机日期(指定范围)
    static double
    Generates a random double [0, 1) 生成随机浮点数 [0, 1)
    static double
    randomDouble(double bound)
    Generates a random double [0, bound) 生成随机浮点数 [0, bound)
    static double
    randomDouble(double origin, double bound)
    Generates a random double [origin, bound) 生成随机浮点数 [origin, bound)
    static <T> T
    Selects a random element from a list 从列表随机选择元素
    static <T> T
    randomElement(T... array)
    Selects a random element from an array 从数组随机选择元素
    static <T> List<T>
    randomElements(List<T> list, int count)
    Selects multiple random elements from a collection 从集合随机选择多个元素
    static int
    randomInt(int bound)
    Generates a random integer [0, bound) 生成随机整数 [0, bound)
    static int
    randomInt(int origin, int bound)
    Generates a random integer [origin, bound) 生成随机整数 [origin, bound)
    static long
    randomLong(long bound)
    Generates a random long [0, bound) 生成随机长整数 [0, bound)
    static long
    randomLong(long origin, long bound)
    Generates a random long [origin, bound) 生成随机长整数 [origin, bound)
    static String
    randomLowerCase(int length)
    Generates a random lowercase string 生成随机小写字母字符串
    static String
    randomNumeric(int length)
    Generates a random numeric string 生成随机数字字符串
    static String
    randomString(int length, String chars)
    Generates a random string (custom character set) 生成随机字符串(自定义字符集)
    static String
    randomUpperCase(int length)
    Generates a random uppercase string 生成随机大写字母字符串
    static String
    secureAlphanumeric(int length)
    Generates a secure random alphanumeric string 生成安全随机字母数字字符串
    static byte[]
    secureBytes(int length)
    Generates a secure random byte array 生成安全随机字节数组
    static int
    secureInt(int bound)
    Generates a secure random integer 生成安全随机整数
    static long
    Generates a secure random long 生成安全随机长整数
    static String
    secureString(int length, String chars)
    Generates a secure random string (custom character set) 生成安全随机字符串(自定义字符集)
    static String
    Generates a secure UUID (based on SecureRandom) 生成安全 UUID(基于 SecureRandom)
    static <T> void
    shuffle(List<T> list)
    Shuffles a list 打乱列表顺序
    static <T> void
    shuffle(T[] array)
    Shuffles an array 打乱数组顺序
    static String
    Generates a simple UUID (without hyphens) 生成简单 UUID(无横线)
    static String
    Generates a UUID 生成 UUID

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • randomInt

      public static int randomInt(int bound)
      Generates a random integer [0, bound) 生成随机整数 [0, bound)
    • randomInt

      public static int randomInt(int origin, int bound)
      Generates a random integer [origin, bound) 生成随机整数 [origin, bound)
    • randomLong

      public static long randomLong(long bound)
      Generates a random long [0, bound) 生成随机长整数 [0, bound)
    • randomLong

      public static long randomLong(long origin, long bound)
      Generates a random long [origin, bound) 生成随机长整数 [origin, bound)
    • randomDouble

      public static double randomDouble()
      Generates a random double [0, 1) 生成随机浮点数 [0, 1)
    • randomDouble

      public static double randomDouble(double bound)
      Generates a random double [0, bound) 生成随机浮点数 [0, bound)
    • randomDouble

      public static double randomDouble(double origin, double bound)
      Generates a random double [origin, bound) 生成随机浮点数 [origin, bound)
    • randomBoolean

      public static boolean randomBoolean()
      Generates a random boolean 生成随机布尔值
    • randomBytes

      public static byte[] randomBytes(int length)
      Generates a random byte array 生成随机字节数组
    • secureBytes

      public static byte[] secureBytes(int length)
      Generates a secure random byte array 生成安全随机字节数组
    • secureInt

      public static int secureInt(int bound)
      Generates a secure random integer 生成安全随机整数
    • secureLong

      public static long secureLong()
      Generates a secure random long 生成安全随机长整数
    • randomAlphanumeric

      public static String randomAlphanumeric(int length)
      Generates a random alphanumeric string 生成随机字母数字字符串
    • secureAlphanumeric

      public static String secureAlphanumeric(int length)
      Generates a secure random alphanumeric string 生成安全随机字母数字字符串
    • randomNumeric

      public static String randomNumeric(int length)
      Generates a random numeric string 生成随机数字字符串
    • randomAlphabetic

      public static String randomAlphabetic(int length)
      Generates a random alphabetic string 生成随机字母字符串
    • randomUpperCase

      public static String randomUpperCase(int length)
      Generates a random uppercase string 生成随机大写字母字符串
    • randomLowerCase

      public static String randomLowerCase(int length)
      Generates a random lowercase string 生成随机小写字母字符串
    • randomString

      public static String randomString(int length, String chars)
      Generates a random string (custom character set) 生成随机字符串(自定义字符集)
    • secureString

      public static String secureString(int length, String chars)
      Generates a secure random string (custom character set) 生成安全随机字符串(自定义字符集)
    • uuid

      public static String uuid()
      Generates a UUID 生成 UUID
    • simpleUUID

      public static String simpleUUID()
      Generates a simple UUID (without hyphens) 生成简单 UUID(无横线)
    • secureUUID

      public static String secureUUID()
      Generates a secure UUID (based on SecureRandom) 生成安全 UUID(基于 SecureRandom)
    • randomElement

      public static <T> T randomElement(List<T> list)
      Selects a random element from a list 从列表随机选择元素
    • randomElement

      @SafeVarargs public static <T> T randomElement(T... array)
      Selects a random element from an array 从数组随机选择元素
    • randomElements

      public static <T> List<T> randomElements(List<T> list, int count)
      Selects multiple random elements from a collection 从集合随机选择多个元素
    • shuffle

      public static <T> void shuffle(List<T> list)
      Shuffles a list 打乱列表顺序
    • shuffle

      public static <T> void shuffle(T[] array)
      Shuffles an array 打乱数组顺序
    • randomDate

      public static LocalDate randomDate(int startYear, int endYear)
      Generates a random date [startYear, endYear) 生成随机日期 [startYear, endYear)
    • randomDate

      public static LocalDate randomDate(LocalDate start, LocalDate end)
      Generates a random date (specified range) 生成随机日期(指定范围)