Class IntProvider
- All Implemented Interfaces:
RandomIntSource, RestorableUniformRandomProvider, UniformRandomProvider
- Direct Known Subclasses:
AbstractPcg6432, AbstractPcgMcg6432, AbstractWell, AbstractXoRoShiRo64, AbstractXoShiRo128, DotyHumphreySmallFastCounting32, ISAACRandom, JDKRandom, JenkinsSmallFast32, KISSRandom, L32X64Mix, MersenneTwister, MiddleSquareWeylSequence, MultiplyWithCarry256
int-based
source randomness.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intProvides a bit source for booleans.private static final intEmpty boolean source. -
Constructor Summary
ConstructorsModifierConstructorDescriptionCreates a new instance.protectedIntProvider(IntProvider source) Creates a new instance copying the state from the source. -
Method Summary
Modifier and TypeMethodDescriptionprivate 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[]Creates a snapshot of the RNG state.booleanGenerates 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.doubleGenerates adoublevalue between 0 (inclusive) and 1 (exclusive).intnextInt()Generates anintvalue.longnextLong()Generates alongvalue.protected voidReset the cached state used in the default implementation ofnextBoolean().protected voidsetStateInternal(byte[] s) Resets the RNG to the givenstate.Methods inherited from class BaseProvider
checkIndex, checkStateSize, composeStateInternal, extendSeed, extendSeed, fillState, fillState, restoreState, saveState, splitStateInternal, toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface RandomIntSource
next
-
Field Details
-
EMPTY_BOOL_SOURCE
private static final int EMPTY_BOOL_SOURCEEmpty boolean source. This is the location of the sign-bit after 31 right shifts on the boolean source.- See Also:
-
booleanSource
private int booleanSourceProvides 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 Details
-
IntProvider
public IntProvider()Creates a new instance. -
IntProvider
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 Details
-
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:
-
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
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:
IndexOutOfBoundsException- if the sub-range is out of bounds
-