Class RepeatableRandom

java.lang.Object
cloud.opencode.base.test.data.RepeatableRandom

public final class RepeatableRandom extends Object
Repeatable Random - Seeded random generator for reproducible tests 可重复随机 - 用于可复现测试的种子随机生成器

Provides deterministic random values based on a seed, useful for creating reproducible test data.

基于种子提供确定性随机值,用于创建可复现的测试数据。

Features | 主要功能:

  • Deterministic random sequences from seed - 基于种子的确定性随机序列
  • Reproducible test data generation - 可复现的测试数据生成
  • Support for int, long, double, boolean, string generation - 支持int、long、double、boolean、字符串生成
  • Reset capability for re-running sequences - 可重置以重新运行序列

Usage Examples | 使用示例:

// Same seed = same sequence
RepeatableRandom r1 = new RepeatableRandom(12345L);
RepeatableRandom r2 = new RepeatableRandom(12345L);

r1.nextInt(100) == r2.nextInt(100); // true

// For debugging, print the seed
RepeatableRandom r = RepeatableRandom.withRandomSeed();
System.out.println("Seed: " + r.getSeed());

Security | 安全性:

  • Thread-safe: No (uses non-thread-safe java.util.Random) - 线程安全: 否(使用非线程安全的java.util.Random)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-test V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    RepeatableRandom(long seed)
    Creates a repeatable random with the specified seed.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Gets the seed.
    boolean
    Returns a random boolean.
    boolean
    nextBoolean(double trueProbability)
    Returns a random boolean with specified true probability.
    nextDigits(int length)
    Returns a random string of digits.
    double
    Returns a random double between 0.0 and 1.0.
    double
    nextDouble(double min, double max)
    Returns a random double between min and max.
    <T> T
    nextElement(T[] array)
    Returns a random element from the array.
    int
    Returns a random int.
    int
    nextInt(int bound)
    Returns a random int between 0 (inclusive) and bound (exclusive).
    int
    nextInt(int min, int max)
    Returns a random int between min (inclusive) and max (inclusive).
    long
    Returns a random long.
    long
    nextLong(long bound)
    Returns a random long between 0 and bound.
    nextString(int length)
    Returns a random string of specified length.
    Resets the random generator to the initial state.
    Creates a repeatable random with a random seed.
    withSeed(long seed)
    Creates a repeatable random with specified seed.

    Methods inherited from class Object

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

    • RepeatableRandom

      public RepeatableRandom(long seed)
      Creates a repeatable random with the specified seed. 使用指定种子创建可重复随机。
      Parameters:
      seed - the seed | 种子
  • Method Details

    • withRandomSeed

      public static RepeatableRandom withRandomSeed()
      Creates a repeatable random with a random seed. 使用随机种子创建可重复随机。
      Returns:
      the repeatable random | 可重复随机
    • withSeed

      public static RepeatableRandom withSeed(long seed)
      Creates a repeatable random with specified seed. 使用指定种子创建可重复随机。
      Parameters:
      seed - the seed | 种子
      Returns:
      the repeatable random | 可重复随机
    • getSeed

      public long getSeed()
      Gets the seed. 获取种子。
      Returns:
      the seed | 种子
    • nextInt

      public int nextInt()
      Returns a random int. 返回随机整数。
      Returns:
      random int | 随机整数
    • nextInt

      public int nextInt(int bound)
      Returns a random int between 0 (inclusive) and bound (exclusive). 返回0(包含)到边界(不包含)之间的随机整数。
      Parameters:
      bound - the upper bound | 上界
      Returns:
      random int | 随机整数
    • nextInt

      public int nextInt(int min, int max)
      Returns a random int between min (inclusive) and max (inclusive). 返回最小值(包含)到最大值(包含)之间的随机整数。
      Parameters:
      min - the minimum value | 最小值
      max - the maximum value | 最大值
      Returns:
      random int | 随机整数
    • nextLong

      public long nextLong()
      Returns a random long. 返回随机长整数。
      Returns:
      random long | 随机长整数
    • nextLong

      public long nextLong(long bound)
      Returns a random long between 0 and bound. 返回0到边界之间的随机长整数。
      Parameters:
      bound - the upper bound | 上界
      Returns:
      random long | 随机长整数
    • nextDouble

      public double nextDouble()
      Returns a random double between 0.0 and 1.0. 返回0.0到1.0之间的随机双精度数。
      Returns:
      random double | 随机双精度数
    • nextDouble

      public double nextDouble(double min, double max)
      Returns a random double between min and max. 返回最小值到最大值之间的随机双精度数。
      Parameters:
      min - the minimum value | 最小值
      max - the maximum value | 最大值
      Returns:
      random double | 随机双精度数
    • nextBoolean

      public boolean nextBoolean()
      Returns a random boolean. 返回随机布尔值。
      Returns:
      random boolean | 随机布尔值
    • nextBoolean

      public boolean nextBoolean(double trueProbability)
      Returns a random boolean with specified true probability. 返回带指定真概率的随机布尔值。
      Parameters:
      trueProbability - the probability of true (0.0 - 1.0) | 真的概率
      Returns:
      random boolean | 随机布尔值
    • nextString

      public String nextString(int length)
      Returns a random string of specified length. 返回指定长度的随机字符串。
      Parameters:
      length - the length | 长度
      Returns:
      random string | 随机字符串
    • nextDigits

      public String nextDigits(int length)
      Returns a random string of digits. 返回随机数字字符串。
      Parameters:
      length - the length | 长度
      Returns:
      random digits | 随机数字
    • nextElement

      public <T> T nextElement(T[] array)
      Returns a random element from the array. 从数组返回随机元素。
      Type Parameters:
      T - the element type | 元素类型
      Parameters:
      array - the array | 数组
      Returns:
      random element | 随机元素
    • reset

      public RepeatableRandom reset()
      Resets the random generator to the initial state. 重置随机生成器到初始状态。
      Returns:
      a new repeatable random with the same seed | 相同种子的新可重复随机