Class CombinationSampler
- All Implemented Interfaces:
ObjectSampler<int[]>,SharedStateObjectSampler<int[]>,SharedStateSampler<SharedStateObjectSampler<int[]>>
A combination is a selection of items from a collection, such that (unlike
permutations) the order of selection does not matter. This
sampler can be used to generate a combination in an unspecified order and is
faster than the corresponding PermutationSampler.
Note that the sample order is unspecified. For example a sample
combination of 2 from 4 may return [0,1] or [1,0] as the two are
equivalent, and the order of a given combination may change in subsequent samples.
The sampler can be used to generate indices to select subsets where the order of the subset is not important.
Sampling uses UniformRandomProvider.nextInt(int).
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int[]Domain of the combination.private final UniformRandomProviderRNG.private final intThe number of steps of a full shuffle to perform.private final booleanThe section to copy the domain from after a partial shuffle. -
Constructor Summary
ConstructorsModifierConstructorDescriptionCombinationSampler(UniformRandomProvider rng, int n, int k) Creates a generator of combinations.privateCombinationSampler(UniformRandomProvider rng, CombinationSampler source) -
Method Summary
Modifier and TypeMethodDescriptionint[]sample()Return a combination ofkwhose entries are selected randomly, without repetition, from the integers 0, 1, ...,n-1 (inclusive).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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.commons.rng.sampling.ObjectSampler
samples, samples
-
Field Details
-
domain
private final int[] domainDomain of the combination. -
steps
private final int stepsThe number of steps of a full shuffle to perform. -
upper
private final boolean upperThe section to copy the domain from after a partial shuffle. -
rng
RNG.
-
-
Constructor Details
-
CombinationSampler
Creates a generator of combinations.The
sample()method will generate an integer array of lengthkwhose entries are selected randomly, without repetition, from the integers 0, 1, ...,n-1 (inclusive). The returned array represents a combination ofntakenk.In contrast to a permutation, the returned array is not guaranteed to be in a random order. The
sample()method returns the array in an unspecified order.If
n <= 0ork <= 0ork > nthen no combination is required and an exception is raised.- Parameters:
rng- Generator of uniformly distributed random numbers.n- Domain of the combination.k- Size of the combination.- Throws:
IllegalArgumentException- ifn <= 0ork <= 0ork > n.
-
CombinationSampler
- Parameters:
rng- Generator of uniformly distributed random numbers.source- Source to copy.
-
-
Method Details
-
sample
public int[] sample()Return a combination ofkwhose entries are selected randomly, without repetition, from the integers 0, 1, ...,n-1 (inclusive).The order of the returned array is not guaranteed to be in a random order as the order of a combination does not matter.
- Specified by:
samplein interfaceObjectSampler<int[]>- Returns:
- a random combination.
-
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<SharedStateObjectSampler<int[]>>- Parameters:
rng- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-