SharedStateSampler<PermutationSampler>
public class PermutationSampler extends java.lang.Object implements SharedStateSampler<PermutationSampler>
Sampling uses UniformRandomProvider.nextInt(int)
.
This class also contains utilities for shuffling an int[]
array in-place.
Constructor | Description |
---|---|
PermutationSampler(org.apache.commons.rng.UniformRandomProvider rng,
int n,
int k) |
Creates a generator of permutations.
|
Modifier and Type | Method | Description |
---|---|---|
static int[] |
natural(int n) |
Creates an array representing the natural number
n . |
int[] |
sample() |
|
static void |
shuffle(org.apache.commons.rng.UniformRandomProvider rng,
int[] list) |
Shuffles the entries of the given array.
|
static void |
shuffle(org.apache.commons.rng.UniformRandomProvider rng,
int[] list,
int start,
boolean towardHead) |
Shuffles the entries of the given array, using the
Fisher-Yates algorithm.
|
PermutationSampler |
withUniformRandomProvider(org.apache.commons.rng.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.
|
public PermutationSampler(org.apache.commons.rng.UniformRandomProvider rng, int n, int k)
The sample()
method will generate an integer array of
length k
whose entries are selected randomly, without
repetition, from the integers 0, 1, ..., n
-1 (inclusive).
The returned array represents a permutation of n
taken
k
.
rng
- Generator of uniformly distributed random numbers.n
- Domain of the permutation.k
- Size of the permutation.java.lang.IllegalArgumentException
- if n <= 0
or k <= 0
or k > n
.public int[] sample()
PermutationSampler(UniformRandomProvider,int,int)
public PermutationSampler withUniformRandomProvider(org.apache.commons.rng.UniformRandomProvider rng)
withUniformRandomProvider
in interface SharedStateSampler<PermutationSampler>
rng
- Generator of uniformly distributed random numbers.public static void shuffle(org.apache.commons.rng.UniformRandomProvider rng, int[] list)
rng
- Random number generator.list
- Array whose entries will be shuffled (in-place).shuffle(UniformRandomProvider,int[],int,boolean)
public static void shuffle(org.apache.commons.rng.UniformRandomProvider rng, int[] list, int start, boolean towardHead)
start
and towardHead
parameters select which part
of the array is randomized and which is left untouched.
Sampling uses UniformRandomProvider.nextInt(int)
.
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 between
start
and either the end (if false
) or the beginning
(if true
) of the array.public static int[] natural(int n)
n
.n
- Natural number.n
-1.
If n == 0
, the returned array is empty.Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.