- Class RandomSubset provides an object that generates a random subset of a set of integers. The original set consists of the integers from 0 to N−1 inclusive. The subset consists of integers chosen at random without replacement from the original set; each member of the original set is equally likely to be chosen. Class RandomSubset is an Iterator that visits the elements of the original set in a random order; each next() method call returns another element of the subset.
Calling the remove(i) method one or more times removes the given integers from the original set. Those integers will not be chosen for the random subset.
Class RandomSubset is layered on top of a pseudorandom number generator (PRNG), an instance of class Random. Each time the next() method is called, one random number is consumed from the underlying PRNG.
Class RandomSubset has two different implementations with different storage requirements. The implementation is specified with a constructor argument.
- The sparse implementation requires less storage when the size of the random subset (the number of next() method calls) is a small fraction of the size of the original set (N).
- The dense implementation requires less storage when the size of the random subset is a large fraction of the size of the original set.