Class JDKRandomWrapper

  • All Implemented Interfaces:
    UniformRandomProvider

    public final class JDKRandomWrapper
    extends Object
    implements UniformRandomProvider
    Wraps a Random instance to implement UniformRandomProvider. All methods from the Random that match those in UniformRandomProvider are used directly.

    This class can be used to wrap an instance of SecureRandom. The SecureRandom class provides cryptographic random number generation. The features available depend on the Java version and platform. Consult the Java documentation for more details.

    Note: Use of java.util.Random is not recommended for applications. There are many other pseudo-random number generators that are statistically superior and often faster (see RandomSource).

    Since:
    1.3
    See Also:
    SecureRandom, RandomSource
    • Constructor Detail

      • JDKRandomWrapper

        public JDKRandomWrapper​(Random rng)
        Create a wrapper around a Random instance.
        Parameters:
        rng - JDK Random instance to which the random number generation is delegated.
    • Method Detail

      • nextBytes

        public void nextBytes​(byte[] bytes)
        Generates byte values and places them into a user-supplied array.

        The number of random bytes produced is equal to the length of the byte array.

        Specified by:
        nextBytes in interface UniformRandomProvider
        Parameters:
        bytes - Byte array in which to put the random bytes. Cannot be null.
      • nextBytes

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

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

        Specified by:
        nextBytes in interface UniformRandomProvider
        Parameters:
        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

        public int nextInt​(int n)
        Generates an int value between 0 (inclusive) and the specified value (exclusive).
        Specified by:
        nextInt in interface UniformRandomProvider
        Parameters:
        n - Bound on the random number to be returned. Must be positive.
        Returns:
        a random int value between 0 (inclusive) and n (exclusive).
      • nextLong

        public long nextLong​(long n)
        Generates a long value between 0 (inclusive) and the specified value (exclusive).
        Specified by:
        nextLong in interface UniformRandomProvider
        Parameters:
        n - Bound on the random number to be returned. Must be positive.
        Returns:
        a random long value between 0 (inclusive) and n (exclusive).
      • nextFloat

        public float nextFloat()
        Generates a float value between 0 (inclusive) and 1 (exclusive).
        Specified by:
        nextFloat in interface UniformRandomProvider
        Returns:
        the next random value between 0 (inclusive) and 1 (exclusive).
      • nextDouble

        public double nextDouble()
        Generates a double value between 0 (inclusive) and 1 (exclusive).
        Specified by:
        nextDouble in interface UniformRandomProvider
        Returns:
        the next random value between 0 (inclusive) and 1 (exclusive).