public final class SequenceSampler extends Object
| Modifier and Type | Method and 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.
|
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.
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.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.
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.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.
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.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.
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.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.
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.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.
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.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.
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.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.
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.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.
T - The type of array elements.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.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, k2)) and it generates O(min(k, n-k)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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, k2)) and it generates O(min(k, n-k)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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, k2)) and it generates O(min(k, n-k)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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, k2)) and it generates O(min(k, n-k)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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, k2)) and it generates O(min(k, n-k)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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, k2)) and it generates O(min(k, n-k)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
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.IllegalArgumentException - if k > source.length()NegativeArraySizeException - if k < 0public 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, k2)) and it generates O(min(k, n-k)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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, k2)) and it generates O(min(k, n-k)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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, k2)) and it generates O(min(k, n-k)) random numbers.
This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying source of randomness.
T - The type of array elements.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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(n-k) 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(n-k) 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(n-k) 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(n-k) 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(n-k) 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(n-k) 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.
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.IllegalArgumentException - if k > source.length()NegativeArraySizeException - if k < 0public 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(n-k) 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(n-k) 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(n-k) 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.
T - The type of array elements.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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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):45-47, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < n-k. 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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):45-47, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < n-k. 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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):45-47, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < n-k. 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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):45-47, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < n-k. 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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):45-47, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < n-k. 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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):45-47, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < n-k. 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.
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.IllegalArgumentException - if k > source.length()NegativeArraySizeException - if k < 0public 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):45-47, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < n-k. 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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):45-47, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < n-k. 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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):45-47, 1982.
The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than sampleReservoir when k < n-k. 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.
T - The type of array elements.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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(k2) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k2 < 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(k2) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k2 < 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(k2) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k2 < 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(k2) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k2 < 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(k2) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k2 < 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(k2) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k2 < 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.
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.IllegalArgumentException - if k > source.length()NegativeArraySizeException - if k < 0public 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(k2) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k2 < 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(k2) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k2 < 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.
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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0public 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(k2) and it generates O(k) random numbers. Thus, it is a better choice than both sampleReservoir and samplePool when k2 < 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.
T - The type of array elements.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.IllegalArgumentException - if k > source.lengthNegativeArraySizeException - if k < 0Copyright © 2005-2020 Vincent A. Cicirello. All rights reserved.