Class SourceOfRandomness

java.lang.Object
com.pholser.junit.quickcheck.random.SourceOfRandomness

public class SourceOfRandomness extends Object
A source of randomness, fed to generators so they can produce random values for property parameters.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Random
     
    private static final BigInteger
     
    private long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Makes a new source of randomness.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    choose(Collection<T> items)
    Gives a random element of the given collection.
    <T> T
    choose(T[] items)
    Gives a random element of the given array.
    protected final Random
    Gives a reference to the JDK-random delegate of this instance.
    nextBigInteger(int numberOfBits)
    Gives a random BigInteger representable by the given number of bits.
    boolean
     
    byte
    nextByte(byte min, byte max)
    Gives a random byte value, uniformly distributed across the interval [min, max].
    void
    nextBytes(byte[] bytes)
     
    byte[]
    nextBytes(int count)
    Gives an array of a given length containing random bytes.
    char
    nextChar(char min, char max)
    Gives a random char value, uniformly distributed across the interval [min, max].
    double
     
    double
    nextDouble(double min, double max)
    Gives a random double value in the interval [min, max).
    Gives a random Duration value, uniformly distributed across the interval [min, max].
    float
     
    float
    nextFloat(float min, float max)
    Gives a random float value in the interval [min, max).
    double
     
    Gives a random Instant value, uniformly distributed across the interval [min, max].
    int
     
    int
    nextInt(int n)
     
    int
    nextInt(int min, int max)
    Gives a random int value, uniformly distributed across the interval [min, max].
    long
     
    long
    nextLong(long min, long max)
    Gives a random long value, uniformly distributed across the interval [min, max].
    private long[]
    nextSecondsAndNanos(long minSeconds, long minNanos, long maxSeconds, long maxNanos)
     
    short
    nextShort(short min, short max)
    Gives a random short value, uniformly distributed across the interval [min, max].
    long
     
    void
    setSeed(long seed)
     
    Gives a JDK source of randomness, with the same internal state as this source of randomness.

    Methods inherited from class java.lang.Object

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

    • NANOS_PER_SECOND

      private static final BigInteger NANOS_PER_SECOND
    • delegate

      private final Random delegate
    • seed

      private long seed
  • Constructor Details

    • SourceOfRandomness

      public SourceOfRandomness(Random delegate)
      Makes a new source of randomness.
      Parameters:
      delegate - a JDK source of randomness, to which the new instance will delegate
  • Method Details

    • toJDKRandom

      public Random toJDKRandom()

      Gives a JDK source of randomness, with the same internal state as this source of randomness.

      Returns:
      a JDK "clone" of self
    • nextBoolean

      public boolean nextBoolean()
      Returns:
      a uniformly distributed boolean value
      See Also:
    • nextBytes

      public void nextBytes(byte[] bytes)
      Parameters:
      bytes - a byte array to fill with random values
      See Also:
    • nextBytes

      public byte[] nextBytes(int count)
      Gives an array of a given length containing random bytes.
      Parameters:
      count - the desired length of the random byte array
      Returns:
      random bytes
      See Also:
    • nextDouble

      public double nextDouble()
      Returns:
      a uniformly distributed random double value in the interval [0.0, 1.0)
      See Also:
    • nextFloat

      public float nextFloat()
      Returns:
      a uniformly distributed random float value in the interval [0.0, 1.0)
      See Also:
    • nextGaussian

      public double nextGaussian()
      Returns:
      a Gaussian-distributed random double value
      See Also:
    • nextInt

      public int nextInt()
      Returns:
      a uniformly distributed random int value
      See Also:
    • nextInt

      public int nextInt(int n)
      Parameters:
      n - upper bound
      Returns:
      a uniformly distributed random int value in the interval [0, n)
      See Also:
    • nextLong

      public long nextLong()
      Returns:
      a uniformly distributed random long value
      See Also:
    • setSeed

      public void setSeed(long seed)
      Parameters:
      seed - value with which to seed this source of randomness
      See Also:
    • seed

      public long seed()
      Returns:
      the value used to initially seed this source of randomness
    • nextByte

      public byte nextByte(byte min, byte max)
      Gives a random byte value, uniformly distributed across the interval [min, max].
      Parameters:
      min - lower bound of the desired interval
      max - upper bound of the desired interval
      Returns:
      a random value
    • nextChar

      public char nextChar(char min, char max)
      Gives a random char value, uniformly distributed across the interval [min, max].
      Parameters:
      min - lower bound of the desired interval
      max - upper bound of the desired interval
      Returns:
      a random value
    • nextDouble

      public double nextDouble(double min, double max)

      Gives a random double value in the interval [min, max).

      This naive implementation takes a random double value from Random.nextDouble() and scales/shifts the value into the desired interval. This may give surprising results for large ranges.

      Parameters:
      min - lower bound of the desired interval
      max - upper bound of the desired interval
      Returns:
      a random value
    • nextFloat

      public float nextFloat(float min, float max)

      Gives a random float value in the interval [min, max).

      This naive implementation takes a random float value from Random.nextFloat() and scales/shifts the value into the desired interval. This may give surprising results for large ranges.

      Parameters:
      min - lower bound of the desired interval
      max - upper bound of the desired interval
      Returns:
      a random value
    • nextInt

      public int nextInt(int min, int max)
      Gives a random int value, uniformly distributed across the interval [min, max].
      Parameters:
      min - lower bound of the desired interval
      max - upper bound of the desired interval
      Returns:
      a random value
    • nextLong

      public long nextLong(long min, long max)
      Gives a random long value, uniformly distributed across the interval [min, max].
      Parameters:
      min - lower bound of the desired interval
      max - upper bound of the desired interval
      Returns:
      a random value
    • nextShort

      public short nextShort(short min, short max)
      Gives a random short value, uniformly distributed across the interval [min, max].
      Parameters:
      min - lower bound of the desired interval
      max - upper bound of the desired interval
      Returns:
      a random value
    • nextBigInteger

      public BigInteger nextBigInteger(int numberOfBits)
      Gives a random BigInteger representable by the given number of bits.
      Parameters:
      numberOfBits - the desired number of bits
      Returns:
      a random BigInteger
      See Also:
    • nextInstant

      public Instant nextInstant(Instant min, Instant max)
      Gives a random Instant value, uniformly distributed across the interval [min, max].
      Parameters:
      min - lower bound of the desired interval
      max - upper bound of the desired interval
      Returns:
      a random value
    • nextDuration

      public Duration nextDuration(Duration min, Duration max)
      Gives a random Duration value, uniformly distributed across the interval [min, max].
      Parameters:
      min - lower bound of the desired interval
      max - upper bound of the desired interval
      Returns:
      a random value
    • choose

      public <T> T choose(Collection<T> items)
      Gives a random element of the given collection.
      Type Parameters:
      T - type of items in the collection
      Parameters:
      items - a collection
      Returns:
      a randomly chosen element from the collection
    • choose

      public <T> T choose(T[] items)
      Gives a random element of the given array.
      Type Parameters:
      T - type of items in the array
      Parameters:
      items - an array
      Returns:
      a randomly chosen element from the array
    • delegate

      protected final Random delegate()
      Gives a reference to the JDK-random delegate of this instance. Intended for subclasses.
      Returns:
      the JDK-random delegate
    • nextSecondsAndNanos

      private long[] nextSecondsAndNanos(long minSeconds, long minNanos, long maxSeconds, long maxNanos)