Package edu.rit.util

Class RandomSample

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

public class RandomSample extends Object
Class RandomSample provides objects that generate random samples from discrete sets.
Version:
15-Feb-2010
Author:
Alan Kaminsky
  • Method Details

    • withoutReplacement

      public static Iterator<Integer> withoutReplacement(Random prng, int n, int N)
      Create an iterator that generates a random sample of ints 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 nN, 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 ints 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 nN, 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 longs 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 nN, 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 longs 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 nN, 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.