Package org.apache.commons.rng.sampling
Class CollectionSampler<T>
- java.lang.Object
-
- org.apache.commons.rng.sampling.CollectionSampler<T>
-
- Type Parameters:
T
- Type of items in the collection.
- All Implemented Interfaces:
ObjectSampler<T>
,SharedStateObjectSampler<T>
,SharedStateSampler<SharedStateObjectSampler<T>>
public class CollectionSampler<T> extends Object implements SharedStateObjectSampler<T>
Sampling from aCollection
.Sampling uses
UniformRandomProvider.nextInt(int)
.- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description CollectionSampler(UniformRandomProvider rng, Collection<T> collection)
Creates a sampler.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description T
sample()
Picks one of the items from thecollection passed to the constructor
.CollectionSampler<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.
-
-
-
Constructor Detail
-
CollectionSampler
public CollectionSampler(UniformRandomProvider rng, Collection<T> collection)
Creates a sampler.- Parameters:
rng
- Generator of uniformly distributed random numbers.collection
- Collection to be sampled. A (shallow) copy will be stored in the created instance.- Throws:
IllegalArgumentException
- ifcollection
is empty.
-
-
Method Detail
-
sample
public T sample()
Picks one of the items from thecollection passed to the constructor
.- Specified by:
sample
in interfaceObjectSampler<T>
- Returns:
- a random sample.
-
withUniformRandomProvider
public CollectionSampler<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.- Specified by:
withUniformRandomProvider
in interfaceSharedStateSampler<T>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-
-