Class UniformRandomProviderSupport


  • final class UniformRandomProviderSupport
    extends java.lang.Object
    Support for UniformRandomProvider default methods.
    Since:
    1.5
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String INVALID_RANGE
      Message format for an invalid range for lower inclusive and upper exclusive.
      private static java.lang.String INVALID_STREAM_SIZE
      Message for an invalid stream size.
      private static java.lang.String INVALID_UPPER_BOUND
      Message for an invalid upper bound (must be positive, finite and above zero).
      private static java.lang.String NULL_ACTION
      Message when the consumer action is null.
      private static long POW_32
      2^32.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static void nextBytes​(UniformRandomProvider source, byte[] bytes, int start, int len)
      Generates random bytes and places them into a user-supplied array.
      (package private) static double nextDouble​(UniformRandomProvider source, double bound)
      Generates a double value between 0 (inclusive) and the specified value (exclusive).
      (package private) static double nextDouble​(UniformRandomProvider source, double origin, double bound)
      Generates a double value between the specified origin (inclusive) and the specified bound (exclusive).
      (package private) static float nextFloat​(UniformRandomProvider source, float bound)
      Generates a float value between 0 (inclusive) and the specified value (exclusive).
      (package private) static float nextFloat​(UniformRandomProvider source, float origin, float bound)
      Generates a float value between the specified origin (inclusive) and the specified bound (exclusive).
      (package private) static int nextInt​(UniformRandomProvider source, int n)
      Generates an int value between 0 (inclusive) and the specified value (exclusive).
      (package private) static int nextInt​(UniformRandomProvider source, int origin, int bound)
      Generates an int value between the specified origin (inclusive) and the specified bound (exclusive).
      (package private) static long nextLong​(UniformRandomProvider source, long n)
      Generates an long value between 0 (inclusive) and the specified value (exclusive).
      (package private) static long nextLong​(UniformRandomProvider source, long origin, long bound)
      Generates a long value between the specified origin (inclusive) and the specified bound (exclusive).
      (package private) static void validateFromIndexSize​(int fromIndex, int size, int length)
      Checks if the sub-range from fromIndex (inclusive) to fromIndex + size (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).
      (package private) static void validateRange​(double origin, double bound)
      Validate the range between the specified origin (inclusive) and the specified bound (exclusive).
      (package private) static void validateRange​(int origin, int bound)
      Validate the range between the specified origin (inclusive) and the specified bound (exclusive).
      (package private) static void validateRange​(long origin, long bound)
      Validate the range between the specified origin (inclusive) and the specified bound (exclusive).
      (package private) static void validateStreamSize​(long size)
      Validate the stream size.
      (package private) static void validateUpperBound​(double bound)
      Validate the upper bound.
      (package private) static void validateUpperBound​(float bound)
      Validate the upper bound.
      (package private) static void validateUpperBound​(int bound)
      Validate the upper bound.
      (package private) static void validateUpperBound​(long bound)
      Validate the upper bound.
      • Methods inherited from class java.lang.Object

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

      • INVALID_STREAM_SIZE

        private static final java.lang.String INVALID_STREAM_SIZE
        Message for an invalid stream size.
        See Also:
        Constant Field Values
      • INVALID_UPPER_BOUND

        private static final java.lang.String INVALID_UPPER_BOUND
        Message for an invalid upper bound (must be positive, finite and above zero).
        See Also:
        Constant Field Values
      • INVALID_RANGE

        private static final java.lang.String INVALID_RANGE
        Message format for an invalid range for lower inclusive and upper exclusive.
        See Also:
        Constant Field Values
      • NULL_ACTION

        private static final java.lang.String NULL_ACTION
        Message when the consumer action is null.
        See Also:
        Constant Field Values
    • Constructor Detail

      • UniformRandomProviderSupport

        private UniformRandomProviderSupport()
        No instances.
    • Method Detail

      • validateStreamSize

        static void validateStreamSize​(long size)
        Validate the stream size.
        Parameters:
        size - Stream size.
        Throws:
        java.lang.IllegalArgumentException - if size is negative.
      • validateUpperBound

        static void validateUpperBound​(int bound)
        Validate the upper bound.
        Parameters:
        bound - Upper bound (exclusive) on the random number to be returned.
        Throws:
        java.lang.IllegalArgumentException - if bound is equal to or less than zero.
      • validateUpperBound

        static void validateUpperBound​(long bound)
        Validate the upper bound.
        Parameters:
        bound - Upper bound (exclusive) on the random number to be returned.
        Throws:
        java.lang.IllegalArgumentException - if bound is equal to or less than zero.
      • validateUpperBound

        static void validateUpperBound​(float bound)
        Validate the upper bound.
        Parameters:
        bound - Upper bound (exclusive) on the random number to be returned.
        Throws:
        java.lang.IllegalArgumentException - if bound is equal to or less than zero, or is not finite
      • validateUpperBound

        static void validateUpperBound​(double bound)
        Validate the upper bound.
        Parameters:
        bound - Upper bound (exclusive) on the random number to be returned.
        Throws:
        java.lang.IllegalArgumentException - if bound is equal to or less than zero, or is not finite
      • validateRange

        static void validateRange​(int origin,
                                  int bound)
        Validate the range between the specified origin (inclusive) and the specified bound (exclusive).
        Parameters:
        origin - Lower bound on the random number to be returned.
        bound - Upper bound (exclusive) on the random number to be returned.
        Throws:
        java.lang.IllegalArgumentException - if origin is greater than or equal to bound.
      • validateRange

        static void validateRange​(long origin,
                                  long bound)
        Validate the range between the specified origin (inclusive) and the specified bound (exclusive).
        Parameters:
        origin - Lower bound on the random number to be returned.
        bound - Upper bound (exclusive) on the random number to be returned.
        Throws:
        java.lang.IllegalArgumentException - if origin is greater than or equal to bound.
      • validateRange

        static void validateRange​(double origin,
                                  double bound)
        Validate the range between the specified origin (inclusive) and the specified bound (exclusive).
        Parameters:
        origin - Lower bound on the random number to be returned.
        bound - Upper bound (exclusive) on the random number to be returned.
        Throws:
        java.lang.IllegalArgumentException - if origin is not finite, or bound is not finite, or origin is greater than or equal to bound.
      • validateFromIndexSize

        static void validateFromIndexSize​(int fromIndex,
                                          int size,
                                          int length)
        Checks if the sub-range from fromIndex (inclusive) to fromIndex + size (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

        This function provides the functionality of java.utils.Objects.checkFromIndexSize introduced in JDK 9. The Objects javadoc has been reproduced for reference.

        The sub-range is defined to be out of bounds if any of the following inequalities is true:

        • fromIndex < 0
        • size < 0
        • fromIndex + size > length, taking into account integer overflow
        • length < 0, which is implied from the former inequalities

        Note: This is not an exact implementation of the functionality of Objects.checkFromIndexSize. The following changes have been made:

        • The method signature has been changed to avoid the return of fromIndex; this value is not used within this package.
        • No checks are made for length < 0 as this is assumed to be derived from an array length.
        Parameters:
        fromIndex - the lower-bound (inclusive) of the sub-interval
        size - the size of the sub-range
        length - the upper-bound (exclusive) of the range
        Throws:
        java.lang.IndexOutOfBoundsException - if the sub-range is out of bounds
      • nextBytes

        static void nextBytes​(UniformRandomProvider source,
                              byte[] bytes,
                              int start,
                              int len)
        Generates random bytes and places them into a user-supplied array.

        The array is filled with bytes extracted from random long values. This implies that the number of random bytes generated may be larger than the length of the byte array.

        Parameters:
        source - Source of randomness.
        bytes - Array in which to put the generated bytes. Cannot be null.
        start - Index at which to start inserting the generated bytes.
        len - Number of bytes to insert.
      • nextInt

        static int nextInt​(UniformRandomProvider source,
                           int n)
        Generates an int value between 0 (inclusive) and the specified value (exclusive).
        Parameters:
        source - Source of randomness.
        n - Bound on the random number to be returned. Must be strictly positive.
        Returns:
        a random int value between 0 (inclusive) and n (exclusive).
      • nextInt

        static int nextInt​(UniformRandomProvider source,
                           int origin,
                           int bound)
        Generates an int value between the specified origin (inclusive) and the specified bound (exclusive).
        Parameters:
        source - Source of randomness.
        origin - Lower bound on the random number to be returned.
        bound - Upper bound (exclusive) on the random number to be returned. Must be above origin.
        Returns:
        a random int value between origin (inclusive) and bound (exclusive).
      • nextLong

        static long nextLong​(UniformRandomProvider source,
                             long n)
        Generates an long value between 0 (inclusive) and the specified value (exclusive).
        Parameters:
        source - Source of randomness.
        n - Bound on the random number to be returned. Must be strictly positive.
        Returns:
        a random long value between 0 (inclusive) and n (exclusive).
      • nextLong

        static long nextLong​(UniformRandomProvider source,
                             long origin,
                             long bound)
        Generates a long value between the specified origin (inclusive) and the specified bound (exclusive).
        Parameters:
        source - Source of randomness.
        origin - Lower bound on the random number to be returned.
        bound - Upper bound (exclusive) on the random number to be returned. Must be above origin.
        Returns:
        a random long value between origin (inclusive) and bound (exclusive).
      • nextFloat

        static float nextFloat​(UniformRandomProvider source,
                               float bound)
        Generates a float value between 0 (inclusive) and the specified value (exclusive).
        Parameters:
        source - Source of randomness.
        bound - Bound on the random number to be returned. Must be strictly positive.
        Returns:
        a random float value between 0 (inclusive) and bound (exclusive).
      • nextFloat

        static float nextFloat​(UniformRandomProvider source,
                               float origin,
                               float bound)
        Generates a float value between the specified origin (inclusive) and the specified bound (exclusive).
        Parameters:
        source - Source of randomness.
        origin - Lower bound on the random number to be returned. Must be finite.
        bound - Upper bound (exclusive) on the random number to be returned. Must be above origin and finite.
        Returns:
        a random float value between origin (inclusive) and bound (exclusive).
      • nextDouble

        static double nextDouble​(UniformRandomProvider source,
                                 double bound)
        Generates a double value between 0 (inclusive) and the specified value (exclusive).
        Parameters:
        source - Source of randomness.
        bound - Bound on the random number to be returned. Must be strictly positive.
        Returns:
        a random double value between 0 (inclusive) and bound (exclusive).
      • nextDouble

        static double nextDouble​(UniformRandomProvider source,
                                 double origin,
                                 double bound)
        Generates a double value between the specified origin (inclusive) and the specified bound (exclusive).
        Parameters:
        source - Source of randomness.
        origin - Lower bound on the random number to be returned. Must be finite.
        bound - Upper bound (exclusive) on the random number to be returned. Must be above origin and finite.
        Returns:
        a random double value between origin (inclusive) and bound (exclusive).