Class SequenceSampler
 java.lang.Object

 org.cicirello.sequences.SequenceSampler

public final class SequenceSampler extends Object
ArraySampler is a class of utility methods related to efficiently generating random samples of array elements, without replacement.


Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]
sample(byte[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.static byte[]
sample(byte[] source, int k, byte[] target)
Generates a random sample of k elements, without replacement, from a given source array.static char[]
sample(char[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.static char[]
sample(char[] source, int k, char[] target)
Generates a random sample of k elements, without replacement, from a given source array.static double[]
sample(double[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.static double[]
sample(double[] source, int k, double[] target)
Generates a random sample of k elements, without replacement, from a given source array.static float[]
sample(float[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.static float[]
sample(float[] source, int k, float[] target)
Generates a random sample of k elements, without replacement, from a given source array.static int[]
sample(int[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.static int[]
sample(int[] source, int k, int[] target)
Generates a random sample of k elements, without replacement, from a given source array.static long[]
sample(long[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.static long[]
sample(long[] source, int k, long[] target)
Generates a random sample of k elements, without replacement, from a given source array.static short[]
sample(short[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.static short[]
sample(short[] source, int k, short[] target)
Generates a random sample of k elements, without replacement, from a given source array.static char[]
sample(String source, double p)
Generates a random sample, without replacement, from a given String with a specified probability of a character's inclusion in the sample.static char[]
sample(String source, int k, char[] target)
Generates a random sample of k chars, without replacement, from a given source String.static <T> T[]
sample(T[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.static <T> T[]
sample(T[] source, int k, T[] target)
Generates a random sample of k elements, without replacement, from a given source array.static byte[]
sampleInsertion(byte[] source, int k, byte[] target)
Generates a random sample of k elements, without replacement, from a given source array.static char[]
sampleInsertion(char[] source, int k, char[] target)
Generates a random sample of k elements, without replacement, from a given source array.static double[]
sampleInsertion(double[] source, int k, double[] target)
Generates a random sample of k elements, without replacement, from a given source array.static float[]
sampleInsertion(float[] source, int k, float[] target)
Generates a random sample of k elements, without replacement, from a given source array.static int[]
sampleInsertion(int[] source, int k, int[] target)
Generates a random sample of k elements, without replacement, from a given source array.static long[]
sampleInsertion(long[] source, int k, long[] target)
Generates a random sample of k elements, without replacement, from a given source array.static short[]
sampleInsertion(short[] source, int k, short[] target)
Generates a random sample of k elements, without replacement, from a given source array.static char[]
sampleInsertion(String source, int k, char[] target)
Generates a random sample of k chars, without replacement, from a given source String.static <T> T[]
sampleInsertion(T[] source, int k, T[] target)
Generates a random sample of k elements, without replacement, from a given source array.static byte[]
samplePool(byte[] source, int k, byte[] target)
Generates a random sample of k elements, without replacement, from a given source array.static char[]
samplePool(char[] source, int k, char[] target)
Generates a random sample of k elements, without replacement, from a given source array.static double[]
samplePool(double[] source, int k, double[] target)
Generates a random sample of k elements, without replacement, from a given source array.static float[]
samplePool(float[] source, int k, float[] target)
Generates a random sample of k elements, without replacement, from a given source array.static int[]
samplePool(int[] source, int k, int[] target)
Generates a random sample of k elements, without replacement, from a given source array.static long[]
samplePool(long[] source, int k, long[] target)
Generates a random sample of k elements, without replacement, from a given source array.static short[]
samplePool(short[] source, int k, short[] target)
Generates a random sample of k elements, without replacement, from a given source array.static char[]
samplePool(String source, int k, char[] target)
Generates a random sample of k chars, without replacement, from a given source String.static <T> T[]
samplePool(T[] source, int k, T[] target)
Generates a random sample of k elements, without replacement, from a given source array.static byte[]
sampleReservoir(byte[] source, int k, byte[] target)
Generates a random sample of k elements, without replacement, from a given source array.static char[]
sampleReservoir(char[] source, int k, char[] target)
Generates a random sample of k elements, without replacement, from a given source array.static double[]
sampleReservoir(double[] source, int k, double[] target)
Generates a random sample of k elements, without replacement, from a given source array.static float[]
sampleReservoir(float[] source, int k, float[] target)
Generates a random sample of k elements, without replacement, from a given source array.static int[]
sampleReservoir(int[] source, int k, int[] target)
Generates a random sample of k elements, without replacement, from a given source array.static long[]
sampleReservoir(long[] source, int k, long[] target)
Generates a random sample of k elements, without replacement, from a given source array.static short[]
sampleReservoir(short[] source, int k, short[] target)
Generates a random sample of k elements, without replacement, from a given source array.static char[]
sampleReservoir(String source, int k, char[] target)
Generates a random sample of k chars, without replacement, from a given source String.static <T> T[]
sampleReservoir(T[] source, int k, T[] target)
Generates a random sample of k elements, without replacement, from a given source array.



Method Detail

sample
public static byte[] sample(byte[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.
 Parameters:
source
 The array from which to sample.p
 The probability that element is included in the sample. The expected sample size is source.length * p. Returns:
 An array containing the sample, whose sample size is simply the length of the array.

sample
public static char[] sample(char[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.
 Parameters:
source
 The array from which to sample.p
 The probability that element is included in the sample. The expected sample size is source.length * p. Returns:
 An array containing the sample, whose sample size is simply the length of the array.

sample
public static double[] sample(double[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.
 Parameters:
source
 The array from which to sample.p
 The probability that element is included in the sample. The expected sample size is source.length * p. Returns:
 An array containing the sample, whose sample size is simply the length of the array.

sample
public static float[] sample(float[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.
 Parameters:
source
 The array from which to sample.p
 The probability that element is included in the sample. The expected sample size is source.length * p. Returns:
 An array containing the sample, whose sample size is simply the length of the array.

sample
public static int[] sample(int[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.
 Parameters:
source
 The array from which to sample.p
 The probability that element is included in the sample. The expected sample size is source.length * p. Returns:
 An array containing the sample, whose sample size is simply the length of the array.

sample
public static long[] sample(long[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.
 Parameters:
source
 The array from which to sample.p
 The probability that element is included in the sample. The expected sample size is source.length * p. Returns:
 An array containing the sample, whose sample size is simply the length of the array.

sample
public static short[] sample(short[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.
 Parameters:
source
 The array from which to sample.p
 The probability that element is included in the sample. The expected sample size is source.length * p. Returns:
 An array containing the sample, whose sample size is simply the length of the array.

sample
public static char[] sample(String source, double p)
Generates a random sample, without replacement, from a given String with a specified probability of a character's inclusion in the sample.
 Parameters:
source
 The String from which to sample.p
 The probability that a character is included in the sample. The expected sample size is source.length() * p. Returns:
 An array containing the sample, whose sample size is simply the length of the array.

sample
public static <T> T[] sample(T[] source, double p)
Generates a random sample, without replacement, from a given source array with a specified probability of an element's inclusion in the sample.
 Type Parameters:
T
 The type of array elements. Parameters:
source
 The array from which to sample.p
 The probability that element is included in the sample. The expected sample size is source.length * p. Returns:
 An array containing the sample, whose sample size is simply the length of the array.

sample
public static int[] sample(int[] source, int k, int[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This method chooses among the ArraySampler.samplePool, ArraySampler.sampleReservoir, and ArraySampler.sampleInsertion methods based on the values of source.length and k.
The runtime is O(min(n, k^{2})) and it generates O(min(k, nk)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sample
public static long[] sample(long[] source, int k, long[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This method chooses among the ArraySampler.samplePool, ArraySampler.sampleReservoir, and ArraySampler.sampleInsertion methods based on the values of source.length and k.
The runtime is O(min(n, k^{2})) and it generates O(min(k, nk)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sample
public static short[] sample(short[] source, int k, short[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This method chooses among the ArraySampler.samplePool, ArraySampler.sampleReservoir, and ArraySampler.sampleInsertion methods based on the values of source.length and k.
The runtime is O(min(n, k^{2})) and it generates O(min(k, nk)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sample
public static byte[] sample(byte[] source, int k, byte[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This method chooses among the ArraySampler.samplePool, ArraySampler.sampleReservoir, and ArraySampler.sampleInsertion methods based on the values of source.length and k.
The runtime is O(min(n, k^{2})) and it generates O(min(k, nk)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sample
public static char[] sample(char[] source, int k, char[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This method chooses among the ArraySampler.samplePool, ArraySampler.sampleReservoir, and ArraySampler.sampleInsertion methods based on the values of source.length and k.
The runtime is O(min(n, k^{2})) and it generates O(min(k, nk)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sample
public static char[] sample(String source, int k, char[] target)
Generates a random sample of k chars, without replacement, from a given source String. All n choose k combinations are equally likely, where n is the length of the source String.
This method chooses among the ArraySampler.samplePool, ArraySampler.sampleReservoir, and ArraySampler.sampleInsertion methods based on the values of source.length and k.
The runtime is O(min(n, k^{2})) and it generates O(min(k, nk)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length()).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.length()NegativeArraySizeException
 if k < 0

sample
public static double[] sample(double[] source, int k, double[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This method chooses among the ArraySampler.samplePool, ArraySampler.sampleReservoir, and ArraySampler.sampleInsertion methods based on the values of source.length and k.
The runtime is O(min(n, k^{2})) and it generates O(min(k, nk)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sample
public static float[] sample(float[] source, int k, float[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This method chooses among the ArraySampler.samplePool, ArraySampler.sampleReservoir, and ArraySampler.sampleInsertion methods based on the values of source.length and k.
The runtime is O(min(n, k^{2})) and it generates O(min(k, nk)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sample
public static <T> T[] sample(T[] source, int k, T[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This method chooses among the ArraySampler.samplePool, ArraySampler.sampleReservoir, and ArraySampler.sampleInsertion methods based on the values of source.length and k.
The runtime is O(min(n, k^{2})) and it generates O(min(k, nk)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Type Parameters:
T
 The type of array elements. Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleReservoir
public static int[] sampleReservoir(int[] source, int k, int[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
Uses the reservoir sampling algorithm (Algorithm R) from J. Vitter's 1985 article "Random Sampling with a Reservoir" from ACM Transactions on Mathematical Software. The runtime is O(n) and it generates O(nk) random numbers. Thus, it is an especially good choice as k approaches n. Only constant extra space required.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleReservoir
public static long[] sampleReservoir(long[] source, int k, long[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
Uses the reservoir sampling algorithm (Algorithm R) from J. Vitter's 1985 article "Random Sampling with a Reservoir" from ACM Transactions on Mathematical Software. The runtime is O(n) and it generates O(nk) random numbers. Thus, it is an especially good choice as k approaches n. Only constant extra space required.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleReservoir
public static short[] sampleReservoir(short[] source, int k, short[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
Uses the reservoir sampling algorithm (Algorithm R) from J. Vitter's 1985 article "Random Sampling with a Reservoir" from ACM Transactions on Mathematical Software. The runtime is O(n) and it generates O(nk) random numbers. Thus, it is an especially good choice as k approaches n. Only constant extra space required.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleReservoir
public static byte[] sampleReservoir(byte[] source, int k, byte[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
Uses the reservoir sampling algorithm (Algorithm R) from J. Vitter's 1985 article "Random Sampling with a Reservoir" from ACM Transactions on Mathematical Software. The runtime is O(n) and it generates O(nk) random numbers. Thus, it is an especially good choice as k approaches n. Only constant extra space required.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleReservoir
public static char[] sampleReservoir(char[] source, int k, char[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
Uses the reservoir sampling algorithm (Algorithm R) from J. Vitter's 1985 article "Random Sampling with a Reservoir" from ACM Transactions on Mathematical Software. The runtime is O(n) and it generates O(nk) random numbers. Thus, it is an especially good choice as k approaches n. Only constant extra space required.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleReservoir
public static char[] sampleReservoir(String source, int k, char[] target)
Generates a random sample of k chars, without replacement, from a given source String. All n choose k combinations are equally likely, where n is the length of the source String.
Uses the reservoir sampling algorithm (Algorithm R) from J. Vitter's 1985 article "Random Sampling with a Reservoir" from ACM Transactions on Mathematical Software. The runtime is O(n) and it generates O(nk) random numbers. Thus, it is an especially good choice as k approaches n. Only constant extra space required.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length()).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.length()NegativeArraySizeException
 if k < 0

sampleReservoir
public static double[] sampleReservoir(double[] source, int k, double[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
Uses the reservoir sampling algorithm (Algorithm R) from J. Vitter's 1985 article "Random Sampling with a Reservoir" from ACM Transactions on Mathematical Software. The runtime is O(n) and it generates O(nk) random numbers. Thus, it is an especially good choice as k approaches n. Only constant extra space required.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleReservoir
public static float[] sampleReservoir(float[] source, int k, float[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
Uses the reservoir sampling algorithm (Algorithm R) from J. Vitter's 1985 article "Random Sampling with a Reservoir" from ACM Transactions on Mathematical Software. The runtime is O(n) and it generates O(nk) random numbers. Thus, it is an especially good choice as k approaches n. Only constant extra space required.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleReservoir
public static <T> T[] sampleReservoir(T[] source, int k, T[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
Uses the reservoir sampling algorithm (Algorithm R) from J. Vitter's 1985 article "Random Sampling with a Reservoir" from ACM Transactions on Mathematical Software. The runtime is O(n) and it generates O(nk) random numbers. Thus, it is an especially good choice as k approaches n. Only constant extra space required.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Type Parameters:
T
 The type of array elements. Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

samplePool
public static int[] samplePool(int[] source, int k, int[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal, 25(1):4547, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < nk. However, this uses O(n) extra space, whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

samplePool
public static long[] samplePool(long[] source, int k, long[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal, 25(1):4547, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < nk. However, this uses O(n) extra space, whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

samplePool
public static short[] samplePool(short[] source, int k, short[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal, 25(1):4547, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < nk. However, this uses O(n) extra space, whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

samplePool
public static byte[] samplePool(byte[] source, int k, byte[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal, 25(1):4547, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < nk. However, this uses O(n) extra space, whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

samplePool
public static char[] samplePool(char[] source, int k, char[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal, 25(1):4547, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < nk. However, this uses O(n) extra space, whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

samplePool
public static char[] samplePool(String source, int k, char[] target)
Generates a random sample of k chars, without replacement, from a given source String. All n choose k combinations are equally likely, where n is the length of the source String.
This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal, 25(1):4547, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < nk. However, this uses O(n) extra space, whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source to sample.k
 The number of random samples (must be no greater than source.length()).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.length()NegativeArraySizeException
 if k < 0

samplePool
public static double[] samplePool(double[] source, int k, double[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal, 25(1):4547, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < nk. However, this uses O(n) extra space, whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

samplePool
public static float[] samplePool(float[] source, int k, float[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal, 25(1):4547, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < nk. However, this uses O(n) extra space, whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

samplePool
public static <T> T[] samplePool(T[] source, int k, T[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal, 25(1):4547, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < nk. However, this uses O(n) extra space, whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Type Parameters:
T
 The type of array elements. Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleInsertion
public static int[] sampleInsertion(int[] source, int k, int[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
The runtime is O(k^{2}) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k^{2} < n. Just like sampleReservoir, the sampleInsertion method only requires O(1) extra space, while samplePool requires O(n) extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleInsertion
public static long[] sampleInsertion(long[] source, int k, long[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
The runtime is O(k^{2}) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k^{2} < n. However, this uses O(k) extra space (better than samplePool), whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleInsertion
public static short[] sampleInsertion(short[] source, int k, short[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
The runtime is O(k^{2}) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k^{2} < n. However, this uses O(k) extra space (better than samplePool), whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleInsertion
public static byte[] sampleInsertion(byte[] source, int k, byte[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
The runtime is O(k^{2}) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k^{2} < n. However, this uses O(k) extra space (better than samplePool), whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleInsertion
public static char[] sampleInsertion(char[] source, int k, char[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
The runtime is O(k^{2}) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k^{2} < n. However, this uses O(k) extra space (better than samplePool), whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleInsertion
public static char[] sampleInsertion(String source, int k, char[] target)
Generates a random sample of k chars, without replacement, from a given source String. All n choose k combinations are equally likely, where n is the length of the source String.
The runtime is O(k^{2}) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k^{2} < n. However, this uses O(k) extra space (better than samplePool), whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source to sample.k
 The number of random samples (must be no greater than source.length()).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.length()NegativeArraySizeException
 if k < 0

sampleInsertion
public static double[] sampleInsertion(double[] source, int k, double[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
The runtime is O(k^{2}) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k^{2} < n. However, this uses O(k) extra space (better than samplePool), whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleInsertion
public static float[] sampleInsertion(float[] source, int k, float[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
The runtime is O(k^{2}) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k^{2} < n. However, this uses O(k) extra space (better than samplePool), whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

sampleInsertion
public static <T> T[] sampleInsertion(T[] source, int k, T[] target)
Generates a random sample of k elements, without replacement, from a given source array. All n choose k combinations are equally likely, where n is the length of the source array.
The runtime is O(k^{2}) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k^{2} < n. However, this uses O(k) extra space (better than samplePool), whereas the reservoir algorithm uses no extra space.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
 Type Parameters:
T
 The type of array elements. Parameters:
source
 The source array to sample.k
 The number of random samples (must be no greater than source.length).target
 An array to hold the result. If target is null or target.length is less than k, then this method will construct a new array for the result. Returns:
 An array containing the random sample.
 Throws:
IllegalArgumentException
 if k > source.lengthNegativeArraySizeException
 if k < 0

