Package org.apache.commons.rng.sampling
Class ListSampler
java.lang.Object
org.apache.commons.rng.sampling.ListSampler
- Since:
- 1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
The size threshold for using the random access algorithm when the list does not implement java.util.RandomAccess. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List
<T> sample
(UniformRandomProvider rng, List<T> collection, int k) Generates a list of sizek
whose entries are selected randomly, without repetition, from the items in the givencollection
.static <T> void
shuffle
(UniformRandomProvider rng, List<T> list) Shuffles the entries of the given array, using the Fisher-Yates algorithm.static <T> void
shuffle
(UniformRandomProvider rng, List<T> list, int start, boolean towardHead) Shuffles the entries of the given array, using the Fisher-Yates algorithm.private static void
Swaps the two specified elements in the array.private static <T> void
Swaps the two specified elements in the list.
-
Field Details
-
RANDOM_ACCESS_SIZE_THRESHOLD
private static final int RANDOM_ACCESS_SIZE_THRESHOLDThe size threshold for using the random access algorithm when the list does not implement java.util.RandomAccess.- See Also:
-
-
Constructor Details
-
ListSampler
private ListSampler()Class contains only static methods.
-
-
Method Details
-
sample
Generates a list of sizek
whose entries are selected randomly, without repetition, from the items in the givencollection
.Sampling is without replacement; but if the source collection contains identical objects, the sample may include repeats.
Sampling uses
UniformRandomProvider.nextInt(int)
.- Type Parameters:
T
- Type of the list items.- Parameters:
rng
- Generator of uniformly distributed random numbers.collection
- List to be sampled from.k
- Size of the returned sample.- Returns:
- a shuffled sample from the source collection.
- Throws:
IllegalArgumentException
- ifk <= 0
ork > collection.size()
.
-
shuffle
Shuffles the entries of the given array, using the Fisher-Yates algorithm.Sampling uses
UniformRandomProvider.nextInt(int)
.- Type Parameters:
T
- Type of the list items.- Parameters:
rng
- Random number generator.list
- List whose entries will be shuffled (in-place).
-
shuffle
public static <T> void shuffle(UniformRandomProvider rng, List<T> list, int start, boolean towardHead) Shuffles the entries of the given array, using the Fisher-Yates algorithm.The
start
andpos
parameters select which part of the array is randomized and which is left untouched.Sampling uses
UniformRandomProvider.nextInt(int)
.- Type Parameters:
T
- Type of the list items.- Parameters:
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 betweenstart
and either the end (iffalse
) or the beginning (iftrue
) of the array.
-
swap
Swaps the two specified elements in the list.- Type Parameters:
T
- Type of the list items.- Parameters:
list
- List.i
- First index.j
- Second index.
-
swap
Swaps the two specified elements in the array.- Parameters:
array
- Array.i
- First index.j
- Second index.
-