Class FastLoadedDiceRollerDiscreteSampler.FLDRSampler
java.lang.Object
org.apache.commons.rng.sampling.distribution.FastLoadedDiceRollerDiscreteSampler
org.apache.commons.rng.sampling.distribution.FastLoadedDiceRollerDiscreteSampler.FLDRSampler
- All Implemented Interfaces:
DiscreteSampler
,SharedStateDiscreteSampler
,SharedStateSampler<SharedStateDiscreteSampler>
- Enclosing class:
FastLoadedDiceRollerDiscreteSampler
private static final class FastLoadedDiceRollerDiscreteSampler.FLDRSampler
extends FastLoadedDiceRollerDiscreteSampler
Class to implement the FLDR sample algorithm.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
Provides a bit source for booleans.private static final int
Empty boolean source.private final int[]
Number of leaf nodes at each level.private final int
Number of levels in the discrete distribution generating (DDG) tree.private final int[]
Stores the leaf node labels in increasing order.private final int
Number of categories.private final UniformRandomProvider
Underlying source of randomness. -
Constructor Summary
ConstructorsModifierConstructorDescription(package private)
FLDRSampler
(UniformRandomProvider rng, int n, int k, int[] h, int[] lH) Creates a sampler.private
Creates a copy with a new source of randomness. -
Method Summary
Methods inherited from class org.apache.commons.rng.sampling.distribution.FastLoadedDiceRollerDiscreteSampler
checkArraySize, indexOfNonZero, of, of, of
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.sampling.distribution.DiscreteSampler
samples, samples
-
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:
-
rng
Underlying source of randomness. -
n
private final int nNumber of categories. -
k
private final int kNumber of levels in the discrete distribution generating (DDG) tree. Equal toceil(log2(m))
wherem
is the sum of observations. -
h
private final int[] hNumber of leaf nodes at each level. -
lH
private final int[] lHStores the leaf node labels in increasing order. NamedH
in the FLDR paper. -
booleanSource
private int booleanSourceProvides a bit source for booleans.A cached value from a call to
UniformRandomProvider.nextInt()
.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
-
FLDRSampler
FLDRSampler(UniformRandomProvider rng, int n, int k, int[] h, int[] lH) Creates a sampler.The input parameters are not validated and must be correctly computed tables.
- Parameters:
rng
- Generator of uniformly distributed random numbers.n
- Number of categoriesk
- Number of levels in the discrete distribution generating (DDG) tree. Equal toceil(log2(m))
wherem
is the sum of observations.h
- Number of leaf nodes at each level.lH
- Stores the leaf node labels in increasing order.
-
FLDRSampler
private FLDRSampler(UniformRandomProvider rng, FastLoadedDiceRollerDiscreteSampler.FLDRSampler source) Creates a copy with a new source of randomness.- Parameters:
rng
- Generator of uniformly distributed random numbers.source
- Source to copy.
-
-
Method Details
-
sample
public int sample()Creates anint
sample.- Returns:
- a sample.
-
flip
private int flip()Provides a source of boolean bits.Note: This replicates the boolean cache functionality of
o.a.c.rng.core.source32.IntProvider
. The method has been simplified to return anint
value rather than aboolean
.- Returns:
- the bit (0 or 1)
-
toString
-
withUniformRandomProvider
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.- Specified by:
withUniformRandomProvider
in interfaceSharedStateSampler<SharedStateDiscreteSampler>
- Specified by:
withUniformRandomProvider
in classFastLoadedDiceRollerDiscreteSampler
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
-