T
- Type of items in the collection.public class DiscreteProbabilityCollectionSampler<T> extends Object implements SharedStateObjectSampler<T>
CollectionSampler
.
Sampling uses UniformRandomProvider.nextDouble()
.
Constructor and Description |
---|
DiscreteProbabilityCollectionSampler(UniformRandomProvider rng,
List<T> collection,
double[] probabilities)
Creates a sampler.
|
DiscreteProbabilityCollectionSampler(UniformRandomProvider rng,
Map<T,Double> collection)
Creates a sampler.
|
Modifier and Type | Method and Description |
---|---|
T |
sample()
Picks one of the items from the collection passed to the constructor.
|
DiscreteProbabilityCollectionSampler<T> |
withUniformRandomProvider(UniformRandomProvider rng)
Create a new instance of the sampler with the same underlying state using the given
uniform random provider as the source of randomness.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
samples, samples
public DiscreteProbabilityCollectionSampler(UniformRandomProvider rng, Map<T,Double> collection)
rng
- Generator of uniformly distributed random numbers.collection
- Collection to be sampled, with the probabilities
associated to each of its items.
A (shallow) copy of the items will be stored in the created instance.
The probabilities must be non-negative, but zero values are allowed
and their sum does not have to equal one (input will be normalized
to make the probabilities sum to one).IllegalArgumentException
- if collection
is empty, a
probability is negative, infinite or NaN
, or the sum of all
probabilities is not strictly positive.public DiscreteProbabilityCollectionSampler(UniformRandomProvider rng, List<T> collection, double[] probabilities)
rng
- Generator of uniformly distributed random numbers.collection
- Collection to be sampled.
A (shallow) copy of the items will be stored in the created instance.probabilities
- Probability associated to each item of the
collection
.
The probabilities must be non-negative, but zero values are allowed
and their sum does not have to equal one (input will be normalized
to make the probabilities sum to one).IllegalArgumentException
- if collection
is empty or
a probability is negative, infinite or NaN
, or if the number
of items in the collection
is not equal to the number of
provided probabilities
.public T sample()
sample
in interface ObjectSampler<T>
public DiscreteProbabilityCollectionSampler<T> withUniformRandomProvider(UniformRandomProvider rng)
withUniformRandomProvider
in interface SharedStateSampler<SharedStateObjectSampler<T>>
rng
- Generator of uniformly distributed random numbers.Copyright © 2016–2022 The Apache Software Foundation. All rights reserved.