## Class RandomSample

- java.lang.Object
- edu.rit.util.RandomSample

public class RandomSampleextends Object

Class RandomSample provides objects that generate random samples from discrete sets.

### Method Summary

Methods Modifier and Type Method and Description `static Iterator<Integer>`

**withoutReplacement**(Random prng, int n, int N)Create an iterator that generates a random sample of`int`s without replacement.`static int`

**withoutReplacement**(Random prng, int n, int N, int[] buf)Generate a random sample of`int`s without replacement.`static Iterator<Long>`

**withoutReplacement**(Random prng, int n, long N)Create an iterator that generates a random sample of`long`s without replacement.`static int`

**withoutReplacement**(Random prng, int n, long N, long[] buf)Generate a random sample of`long`s without replacement.

### Method Detail

#### withoutReplacement

public static Iterator<Integer> withoutReplacement(Random prng, int n, int N)

Create an iterator that generates a random sample of`int`s 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 given`prng`to sample items at random. For each item returned, the iterator consumes one random number from the`prng`.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

public static int withoutReplacement(Random prng, int n, int N, int[] buf)

Generate a random sample of`int`s 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 given`buf`starting at index 0. The iterator uses the given`prng`to sample items at random. For each item sampled, the iterator consumes one random number from the`prng`.As a special case, if

*n*≥*N*, then all items in the set are stored in ascending order in the given`buf`starting at index 0, and the iterator does not use the`prng`.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 if`buf`is null.`IndexOutOfBoundsException`

- (unchecked exception) Thrown if`buf.length`<`n`.

#### withoutReplacement

public static Iterator<Long> withoutReplacement(Random prng, int n, long N)

Create an iterator that generates a random sample of`long`s 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 given`prng`to sample items at random. For each item returned, the iterator consumes one random number from the`prng`.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

public static int withoutReplacement(Random prng, int n, long N, long[] buf)

Generate a random sample of`long`s 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 given`buf`starting at index 0. The iterator uses the given`prng`to sample items at random. For each item sampled, the iterator consumes one random number from the`prng`.As a special case, if

*n*≥*N*, then all items in the set are stored in ascending order in the given`buf`starting at index 0, and the iterator does not use the`prng`.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 if`buf`is null.`IndexOutOfBoundsException`

- (unchecked exception) Thrown if`buf.length`<`n`.

**SCaVis 2.1 © jWork.ORG**