Class RandomSample
- Version:
- 15-Feb-2010
- Author:
- Alan Kaminsky
-
Method Summary
Modifier and TypeMethodDescriptionwithoutReplacement(Random prng, int n, int N) Create an iterator that generates a random sample ofints without replacement.static intwithoutReplacement(Random prng, int n, int N, int[] buf) Generate a random sample ofints without replacement.withoutReplacement(Random prng, int n, long N) Create an iterator that generates a random sample oflongs without replacement.static intwithoutReplacement(Random prng, int n, long N, long[] buf) Generate a random sample oflongs without replacement.
-
Method Details
-
withoutReplacement
Create an iterator that generates a random sample ofints without replacement. The set to be sampled consists of the integers from 0 through N−1 inclusive. The sample consists of n items. Each item in the set is equally likely to occur in the sample. The iterator returns the items in the sample in ascending order. The iterator uses the givenprngto sample items at random. For each item returned, the iterator consumes one random number from theprng.As a special case, if n ≥ N, then the iterator returns all items in the set in ascending order, and the iterator does not use the
prng.The iterator uses the algorithm in A. Bissell, Ordered random selection without replacement, Applied Statistics, 35(1):73-75, 1986.
- Parameters:
prng- Pseudorandom number generator.n- Number of items in the sample.N- Number of items in the set.- Returns:
- Iterator over the sample.
- Throws:
IllegalArgumentException- (unchecked exception) Thrown if n < 0 or N < 0.
-
withoutReplacement
Generate a random sample ofints without replacement. The set to be sampled consists of the integers from 0 through N−1 inclusive. The sample consists of n items. Each item in the set is equally likely to occur in the sample. The items in the sample are stored in ascending order in the givenbufstarting at index 0. The iterator uses the givenprngto sample items at random. For each item sampled, the iterator consumes one random number from theprng.As a special case, if n ≥ N, then all items in the set are stored in ascending order in the given
bufstarting at index 0, and the iterator does not use theprng.This method uses the algorithm in A. Bissell, Ordered random selection without replacement, Applied Statistics, 35(1):73-75, 1986.
- Parameters:
prng- Pseudorandom number generator.n- Number of items in the sample.N- Number of items in the set.buf- Array in which to store the sampled items.- Returns:
- Number of sampled items actually stored in
buf. - Throws:
IllegalArgumentException- (unchecked exception) Thrown if n < 0 or N < 0.NullPointerException- (unchecked exception) Thrown ifbufis null.IndexOutOfBoundsException- (unchecked exception) Thrown ifbuf.length<n.
-
withoutReplacement
Create an iterator that generates a random sample oflongs without replacement. The set to be sampled consists of the integers from 0 through N−1 inclusive. The sample consists of n items. Each item in the set is equally likely to occur in the sample. The iterator returns the items in the sample in ascending order. The iterator uses the givenprngto sample items at random. For each item returned, the iterator consumes one random number from theprng.As a special case, if n ≥ N, then the iterator returns all items in the set in ascending order, and the iterator does not use the
prng.The iterator uses the algorithm in A. Bissell, Ordered random selection without replacement, Applied Statistics, 35(1):73-75, 1986.
- Parameters:
prng- Pseudorandom number generator.n- Number of items in the sample.N- Number of items in the set.- Returns:
- Iterator over the sample.
- Throws:
IllegalArgumentException- (unchecked exception) Thrown if n < 0 or N < 0.
-
withoutReplacement
Generate a random sample oflongs without replacement. The set to be sampled consists of the integers from 0 through N−1 inclusive. The sample consists of n items. Each item in the set is equally likely to occur in the sample. The items in the sample are stored in ascending order in the givenbufstarting at index 0. The iterator uses the givenprngto sample items at random. For each item sampled, the iterator consumes one random number from theprng.As a special case, if n ≥ N, then all items in the set are stored in ascending order in the given
bufstarting at index 0, and the iterator does not use theprng.This method uses the algorithm in A. Bissell, Ordered random selection without replacement, Applied Statistics, 35(1):73-75, 1986.
- Parameters:
prng- Pseudorandom number generator.n- Number of items in the sample.N- Number of items in the set.buf- Array in which to store the sampled items.- Returns:
- Number of sampled items actually stored in
buf. - Throws:
IllegalArgumentException- (unchecked exception) Thrown if n < 0 or N < 0.NullPointerException- (unchecked exception) Thrown ifbufis null.IndexOutOfBoundsException- (unchecked exception) Thrown ifbuf.length<n.
-