|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectedu.rit.util.RandomSubset
public class RandomSubset
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.
| Constructor Summary | |
|---|---|
RandomSubset(Random prng,
int N)
Construct a new random subset object for the original set consisting of the integers from 0 through N−1 inclusive. |
|
RandomSubset(Random prng,
int N,
boolean dense)
Construct a new random subset object for the original set consisting of the integers from 0 through N−1 inclusive. |
|
| Method Summary | |
|---|---|
boolean |
hasNext()
Determine whether there are more integers in the random subset. |
Integer |
next()
Returns the next integer in the random subset. |
void |
remove()
Unsupported operation. |
RandomSubset |
remove(int i)
Remove the given integer from the original set. |
void |
restart()
Restart this random subset's iteration. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public RandomSubset(Random prng,
int N)
prng - Underlying PRNG.N - Size of original set.
NullPointerException - (unchecked exception) Thrown if prng is null.
IllegalArgumentException - (unchecked exception) Thrown if N < 0.
public RandomSubset(Random prng,
int N,
boolean dense)
prng - Underlying PRNG.N - Size of original set.dense - False to use the sparse implementation, true to use the
dense implementation.
NullPointerException - (unchecked exception) Thrown if prng is null.
IllegalArgumentException - (unchecked exception) Thrown if N < 0.| Method Detail |
|---|
public boolean hasNext()
hasNext in interface Iterator<Integer>public Integer next()
next in interface Iterator<Integer>NoSuchElementException - (unchecked exception) Thrown if all the integers in the original set
have been used up.public void remove()
remove in interface Iterator<Integer>UnsupportedOperationException - (unchecked exception) Thrown always.public RandomSubset remove(int i)
If i has already been removed from the original set, either by a remove(i) method call, or by a next() method call that returned i, then this method does nothing.
i - Integer to remove.
IllegalArgumentException - (unchecked exception) Thrown if i is not in the range 0
through N−1 inclusive.public void restart()
The restart() method lets you generate multiple random subsets from the same original set using the same RandomSubset object. This avoids the multiple storage allocations that would take place when creating multiple RandomSubset objects. This in turn can reduce the running time, especially when N is large.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||