I am trying to create a subclass of the Random class in Java that does not repeat its output, and here is what I've got so far ..
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
public class NonRepetitiveRandom extends java.util.Random {
private Set<Integer> noUseInts = new CopyOnWriteArraySet<Integer>();
private static final long serialVersionUID = 1L;
@Override
protected int next(int bits) {
int i;
for (i = super.next(bits); noUseInts.contains(i); i = super.next(bits))
;
noUseInts.add(i);
return i;
}
}
Is it fine ? Do I need to add anything ? I initially tried overriding each next* method using the same model as above, but after noticing that the next method itself is used in all next* methods, I am trying to override it alone.
Note: The shuffling method will not work, because it creates a whole list and shuffles it every time a value is demanded. Also, it wouldn't be practical if ranges are wide and if no ranges are specified at all. In fact, the only use case for this method is when I already have a list at hand. As a conclusion, a non-repeating random number generator would be the most practical solution.