public final class ListSampler extends java.lang.Object
List
.
This class also contains utilities for shuffling a
List
in-place.
Modifier and Type | Method | Description |
---|---|---|
static <T> java.util.List<T>
|
sample(org.apache.commons.rng.UniformRandomProvider rng,
java.util.List<T> collection,
int k)
|
Generates a list of size
k whose entries
are selected randomly, without repetition, from the
items in the given collection .
|
static <T> void
|
shuffle(org.apache.commons.rng.UniformRandomProvider rng,
java.util.List<T> list)
|
Shuffles the entries of the given array, using the
Fisher-Yates
algorithm.
|
static <T> void
|
shuffle(org.apache.commons.rng.UniformRandomProvider rng,
java.util.List<T> list, int start,
boolean towardHead)
|
Shuffles the entries of the given array, using the
Fisher-Yates
algorithm.
|
public static <T> java.util.List<T> sample(org.apache.commons.rng.UniformRandomProvider rng, java.util.List<T> collection, int k)
k
whose entries are
selected randomly, without repetition, from the items in
the given collection
.
Sampling is without replacement; but if the source collection contains identical objects, the sample may include repeats.
Sampling uses
UniformRandomProvider.nextInt(int)
.
T
- Type of the list items.rng
- Generator of uniformly distributed
random numbers.
collection
- List to be sampled from.
k
- Size of the returned sample.java.lang.IllegalArgumentException
- if
k <= 0
or
k > collection.size()
.
public static <T> void shuffle(org.apache.commons.rng.UniformRandomProvider rng, java.util.List<T> list)
Sampling uses
UniformRandomProvider.nextInt(int)
.
T
- Type of the list items.rng
- Random number generator.list
- List whose entries will be shuffled
(in-place).
public static <T> void shuffle(org.apache.commons.rng.UniformRandomProvider rng, java.util.List<T> list, int start, boolean towardHead)
The start
and pos
parameters
select which part of the array is randomized and which
is left untouched.
Sampling uses
UniformRandomProvider.nextInt(int)
.
T
- Type of the list items.rng
- Random number generator.list
- List 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.
Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.