Class JDKRandomWrapper

  • All Implemented Interfaces:
    UniformRandomProvider

    public final class JDKRandomWrapper
    extends java.lang.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
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Random rng
      The JDK Random instance.
    • Constructor Summary

      Constructors 
      Constructor Description
      JDKRandomWrapper​(java.util.Random rng)
      Create a wrapper around a Random instance.
    • Field Detail

      • rng

        private final java.util.Random rng
        The JDK Random instance.
    • Constructor Detail

      • JDKRandomWrapper

        public JDKRandomWrapper​(java.util.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()
        Generates an int value.
        Specified by:
        nextInt in interface UniformRandomProvider
        Returns:
        the next random value.
      • 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()
        Generates a long value.
        Specified by:
        nextLong in interface UniformRandomProvider
        Returns:
        the next random value.
      • 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).
      • nextBoolean

        public boolean nextBoolean()
        Generates a boolean value.
        Specified by:
        nextBoolean in interface UniformRandomProvider
        Returns:
        the next random value.
      • 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).