Class IntProvider

    • Field Detail

      • EMPTY_BOOL_SOURCE

        private static final int EMPTY_BOOL_SOURCE
        Empty boolean source. This is the location of the sign-bit after 31 right shifts on the boolean source.
        See Also:
        Constant Field Values
      • booleanSource

        private int booleanSource
        Provides a bit source for booleans.

        A cached value from a call to RandomIntSource.next().

        Only stores 31-bits when full as 1 bit has already been consumed. The sign bit is a flag that shifts down so the source eventually equals 1 when all bits are consumed and will trigger a refill.

    • Constructor Detail

      • IntProvider

        public IntProvider()
        Creates a new instance.
      • IntProvider

        protected IntProvider​(IntProvider source)
        Creates a new instance copying the state from the source.

        This provides base functionality to allow a generator to create a copy, for example for use in the JumpableUniformRandomProvider interface.

        Parameters:
        source - Source to copy.
        Since:
        1.3
    • Method Detail

      • resetCachedState

        protected void resetCachedState()
        Reset the cached state used in the default implementation of nextBoolean().

        This should be used when the state is no longer valid, for example after a jump performed for the JumpableUniformRandomProvider interface.

        Since:
        1.3
      • getStateInternal

        protected byte[] getStateInternal()
        Creates a snapshot of the RNG state.
        Overrides:
        getStateInternal in class BaseProvider
        Returns:
        the internal state.
      • nextInt

        public int nextInt()
        Generates an int value.
        Specified by:
        nextInt in interface UniformRandomProvider
        Returns:
        the next random value.
      • nextBoolean

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

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

        static void nextBytesFill​(RandomIntSource 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 int 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.
      • checkFromIndexSize

        private static int checkFromIndexSize​(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
        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
        Returns:
        the fromIndex
        Throws:
        java.lang.IndexOutOfBoundsException - if the sub-range is out of bounds