Package org.apache.commons.rng.sampling
Class PermutationSampler
java.lang.Object
org.apache.commons.rng.sampling.PermutationSampler
- All Implemented Interfaces:
ObjectSampler<int[]>
,SharedStateObjectSampler<int[]>
,SharedStateSampler<SharedStateObjectSampler<int[]>>
Class for representing permutations
of a sequence of integers.
Sampling uses UniformRandomProvider.nextInt(int)
.
This class also contains utilities for shuffling an int[]
array in-place.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int[]
Domain of the permutation.private final UniformRandomProvider
RNG.private final int
Size of the permutation. -
Constructor Summary
ConstructorsModifierConstructorDescriptionPermutationSampler
(UniformRandomProvider rng, int n, int k) Creates a generator of permutations.private
PermutationSampler
(UniformRandomProvider rng, PermutationSampler source) -
Method Summary
Modifier and TypeMethodDescriptionstatic int[]
natural
(int n) Creates an array representing the natural numbern
.int[]
sample()
Create an object sample.static void
shuffle
(UniformRandomProvider rng, int[] list) Shuffles the entries of the given array.static void
shuffle
(UniformRandomProvider rng, int[] list, int start, boolean towardHead) Shuffles the entries of the given array, using the Fisher-Yates algorithm.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, wait
Methods inherited from interface org.apache.commons.rng.sampling.ObjectSampler
samples, samples
-
Field Details
-
domain
private final int[] domainDomain of the permutation. -
size
private final int sizeSize of the permutation. -
rng
RNG.
-
-
Constructor Details
-
PermutationSampler
Creates a generator of permutations.The
sample()
method will generate an integer array of lengthk
whose entries are selected randomly, without repetition, from the integers 0, 1, ...,n
-1 (inclusive). The returned array represents a permutation ofn
takenk
.- Parameters:
rng
- Generator of uniformly distributed random numbers.n
- Domain of the permutation.k
- Size of the permutation.- Throws:
IllegalArgumentException
- ifn <= 0
ork <= 0
ork > n
.
-
PermutationSampler
- Parameters:
rng
- Generator of uniformly distributed random numbers.source
- Source to copy.
-
-
Method Details
-
sample
public int[] sample()Description copied from interface:ObjectSampler
Create an object sample.- Specified by:
sample
in interfaceObjectSampler<int[]>
- Returns:
- a random permutation.
- See Also:
-
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<SharedStateObjectSampler<int[]>>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-
shuffle
Shuffles the entries of the given array.- Parameters:
rng
- Random number generator.list
- Array whose entries will be shuffled (in-place).- See Also:
-
shuffle
Shuffles the entries of the given array, using the Fisher-Yates algorithm. Thestart
andtowardHead
parameters select which part of the array is randomized and which is left untouched.Sampling uses
UniformRandomProvider.nextInt(int)
.- Parameters:
rng
- Random number generator.list
- Array whose entries will be shuffled (in-place).start
- Index at which shuffling begins.towardHead
- Shuffling is performed for index positions betweenstart
and either the end (iffalse
) or the beginning (iftrue
) of the array.
-
natural
public static int[] natural(int n) Creates an array representing the natural numbern
.- Parameters:
n
- Natural number.- Returns:
- an array whose entries are the numbers 0, 1, ...,
n
-1. Ifn == 0
, the returned array is empty.
-