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 int
Provides a bit source for booleans.private static final int
Empty boolean source. -
Constructor Summary
ConstructorsModifierConstructorDescriptionCreates a new instance.protected
IntProvider
(IntProvider source) Creates a new instance copying the state from the source. -
Method Summary
Modifier and TypeMethodDescriptionprivate 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).protected byte[]
Creates a snapshot of the RNG state.boolean
Generates aboolean
value.void
nextBytes
(byte[] bytes) Generatesbyte
values and places them into a user-supplied array.void
nextBytes
(byte[] bytes, int start, int len) Generatesbyte
values and places them into a user-supplied array.(package private) static void
nextBytesFill
(RandomIntSource source, byte[] bytes, int start, int len) Generates random bytes and places them into a user-supplied array.double
Generates adouble
value between 0 (inclusive) and 1 (exclusive).int
nextInt()
Generates anint
value.long
nextLong()
Generates along
value.protected void
Reset the cached state used in the default implementation ofnextBoolean()
.protected void
setStateInternal
(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 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
JumpableUniformRandomProvider
interface.- 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
JumpableUniformRandomProvider
interface.- Since:
- 1.3
-
getStateInternal
protected byte[] getStateInternal()Creates a snapshot of the RNG state.- Overrides:
getStateInternal
in classBaseProvider
- Returns:
- the internal state.
-
setStateInternal
protected void setStateInternal(byte[] s) Resets the RNG to the givenstate
.- Overrides:
setStateInternal
in classBaseProvider
- Parameters:
s
- State (previously obtained by a call toBaseProvider.getStateInternal()
).- See Also:
-
nextInt
public int nextInt()Generates anint
value.- Specified by:
nextInt
in interfaceUniformRandomProvider
- Returns:
- the next random value.
-
nextBoolean
public boolean nextBoolean()Generates aboolean
value.- Specified by:
nextBoolean
in interfaceUniformRandomProvider
- Returns:
- the next random value.
-
nextDouble
public double nextDouble()Generates adouble
value between 0 (inclusive) and 1 (exclusive).- Specified by:
nextDouble
in interfaceUniformRandomProvider
- Returns:
- the next random value between 0 (inclusive) and 1 (exclusive).
-
nextLong
public long nextLong()Generates along
value.- Specified by:
nextLong
in interfaceUniformRandomProvider
- Returns:
- the next random value.
-
nextBytes
public void nextBytes(byte[] bytes) Generatesbyte
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 interfaceUniformRandomProvider
- Parameters:
bytes
- Byte array in which to put the random bytes. Cannot benull
.
-
nextBytes
public void nextBytes(byte[] bytes, int start, int len) Generatesbyte
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 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
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 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
-