Class IntProvider
- java.lang.Object
-
- org.apache.commons.rng.core.BaseProvider
-
- org.apache.commons.rng.core.source32.IntProvider
-
- All Implemented Interfaces:
RandomIntSource,RestorableUniformRandomProvider,UniformRandomProvider
- Direct Known Subclasses:
AbstractPcg6432,AbstractPcgMcg6432,AbstractWell,AbstractXoRoShiRo64,AbstractXoShiRo128,DotyHumphreySmallFastCounting32,ISAACRandom,JDKRandom,JenkinsSmallFast32,KISSRandom,L32X64Mix,MersenneTwister,MiddleSquareWeylSequence,MultiplyWithCarry256
public abstract class IntProvider extends BaseProvider implements RandomIntSource
Base class for all implementations that provide anint-based source randomness.
-
-
Field Summary
Fields Modifier and Type Field Description private intbooleanSourceProvides a bit source for booleans.private static intEMPTY_BOOL_SOURCEEmpty boolean source.
-
Constructor Summary
Constructors Modifier Constructor Description IntProvider()Creates a new instance.protectedIntProvider(IntProvider source)Creates a new instance copying the state from the source.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static intcheckFromIndexSize(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).protected byte[]getStateInternal()Creates a snapshot of the RNG state.booleannextBoolean()Generates abooleanvalue.voidnextBytes(byte[] bytes)Generatesbytevalues and places them into a user-supplied array.voidnextBytes(byte[] bytes, int start, int len)Generatesbytevalues and places them into a user-supplied array.(package private) static voidnextBytesFill(RandomIntSource source, byte[] bytes, int start, int len)Generates random bytes and places them into a user-supplied array.doublenextDouble()Generates adoublevalue between 0 (inclusive) and 1 (exclusive).intnextInt()Generates anintvalue.longnextLong()Generates alongvalue.protected voidresetCachedState()Reset the cached state used in the default implementation ofnextBoolean().protected voidsetStateInternal(byte[] s)Resets the RNG to the givenstate.-
Methods inherited from class org.apache.commons.rng.core.BaseProvider
checkIndex, checkStateSize, composeStateInternal, extendSeed, extendSeed, fillState, fillState, restoreState, saveState, splitStateInternal, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.rng.core.source32.RandomIntSource
next
-
-
-
-
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
JumpableUniformRandomProviderinterface.- Parameters:
source- Source to copy.- Since:
- 1.3
-
-
Method Detail
-
resetCachedState
protected void resetCachedState()
Reset the cached state used in the default implementation ofnextBoolean().This should be used when the state is no longer valid, for example after a jump performed for the
JumpableUniformRandomProviderinterface.- Since:
- 1.3
-
getStateInternal
protected byte[] getStateInternal()
Creates a snapshot of the RNG state.- Overrides:
getStateInternalin classBaseProvider- Returns:
- the internal state.
-
setStateInternal
protected void setStateInternal(byte[] s)
Resets the RNG to the givenstate.- Overrides:
setStateInternalin classBaseProvider- Parameters:
s- State (previously obtained by a call toBaseProvider.getStateInternal()).- See Also:
BaseProvider.checkStateSize(byte[],int)
-
nextInt
public int nextInt()
Generates anintvalue.- Specified by:
nextIntin interfaceUniformRandomProvider- Returns:
- the next random value.
-
nextBoolean
public boolean nextBoolean()
Generates abooleanvalue.- Specified by:
nextBooleanin interfaceUniformRandomProvider- Returns:
- the next random value.
-
nextDouble
public double nextDouble()
Generates adoublevalue between 0 (inclusive) and 1 (exclusive).- Specified by:
nextDoublein interfaceUniformRandomProvider- Returns:
- the next random value between 0 (inclusive) and 1 (exclusive).
-
nextLong
public long nextLong()
Generates alongvalue.- Specified by:
nextLongin interfaceUniformRandomProvider- Returns:
- the next random value.
-
nextBytes
public void nextBytes(byte[] bytes)
Generatesbytevalues 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:
nextBytesin interfaceUniformRandomProvider- Parameters:
bytes- Byte array in which to put the random bytes. Cannot benull.
-
nextBytes
public void nextBytes(byte[] bytes, int start, int len)Generatesbytevalues 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:
nextBytesin interfaceUniformRandomProvider- Parameters:
bytes- Array in which to put the generated bytes. Cannot benull.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
intvalues. 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.checkFromIndexSizeintroduced 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 < 0size < 0fromIndex + size > length, taking into account integer overflowlength < 0, which is implied from the former inequalities
- Parameters:
fromIndex- the lower-bound (inclusive) of the sub-intervalsize- the size of the sub-rangelength- the upper-bound (exclusive) of the range- Returns:
- the fromIndex
- Throws:
java.lang.IndexOutOfBoundsException- if the sub-range is out of bounds
-
-