Class AliasMethodDiscreteSampler.SmallTableAliasMethodDiscreteSampler
- All Implemented Interfaces:
DiscreteSampler, SharedStateDiscreteSampler, SharedStateSampler<SharedStateDiscreteSampler>
- Enclosing class:
AliasMethodDiscreteSampler
bits = obtained required number of random bits v = (some of the bits) * constant1 j = (rest of the bits) * constant2 if v < prob[j] then return j else return alias[j]
This is a variant because the bits are not multiplied by constants. In the case of
v the constant is a scale that is pre-applied to the probability table. In the
case of j the constant is not used to scale a deviate to an index; the index is
from a power-of-2 range and so the bits are used directly.
This is implemented using up to 64 bits from the random generator. The index for the table is computed using a mask to extract up to 11 of the lower bits from an integer. The probability is computed using a second integer combined with the remaining bits to create 53-bits for the numerator of a fraction with denominator 253. This is only computed on demand.
Note: This supports a table size of up to 2^11, or 2048, exclusive. Any larger requires
consuming more than 64-bits and the algorithm is not more efficient than the
AliasMethodDiscreteSampler.
Sampling uses 1 or 2 calls to UniformRandomProvider.nextInt().
- Since:
- 1.3
-
Field Summary
FieldsFields inherited from class AliasMethodDiscreteSampler
alias, probability, rng -
Constructor Summary
ConstructorsConstructorDescriptionSmallTableAliasMethodDiscreteSampler(UniformRandomProvider rng, long[] probability, int[] alias) Create a new instance. -
Method Summary
Modifier and TypeMethodDescriptionintsample()Creates anintsample.Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.Methods inherited from class AliasMethodDiscreteSampler
of, of, toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface DiscreteSampler
samples, samples
-
Field Details
-
mask
private final int maskThe mask to isolate the lower bits.
-
-
Constructor Details
-
SmallTableAliasMethodDiscreteSampler
SmallTableAliasMethodDiscreteSampler(UniformRandomProvider rng, long[] probability, int[] alias) Create a new instance.- Parameters:
rng- Generator of uniformly distributed random numbers.probability- Probability table.alias- Alias table.
-
-
Method Details
-
sample
public int sample()Description copied from class:AliasMethodDiscreteSamplerCreates anintsample.- Specified by:
samplein interfaceDiscreteSampler- Overrides:
samplein classAliasMethodDiscreteSampler- Returns:
- a sample.
-
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:
withUniformRandomProviderin interfaceSharedStateSampler<SharedStateDiscreteSampler>- Overrides:
withUniformRandomProviderin classAliasMethodDiscreteSampler- Parameters:
rng- Generator of uniformly distributed random numbers.- Returns:
- the sampler
-