Does maximal-length LFSR (e.g., PRBS sequences) fits?
For instance, the PRBS31 sequence (x31+x28+1) is guaranteed to generate every 31-bit integer (except 0) only once in a cycle (which is 231-1 bits long)
It is fairly easy to implement, too:
public int prbs31(int state) {
int feedback = ((state >> 30) ^ (state >> 27)) & 1;
return ((state << 1) | feedback) & 0xffffffff;
}
You start with some (non-zero!) integer, and call prbs31
successively - passing the previous result back in. (it's a feedback register!)
PRBS31 generates very good statistically-random bit patterns (which is not to be confused with truly random bit patterns)
However, keep in mind that adjacent values will be very similar - the method suggested above does a one-bit sliding-window over the PRBS sequence, meaning every adjacent values have 30-bits in common (though in different places)
But we can advance the register 31 steps each time, like so:
// initial seed, doesn't really matter which value you use
// as long as it's not zero (remember that the sequence goes over
// all the possible 31-bits values anyway)
private static final int SEED = 0x55555555;
private int state = SEED;
public int nextInt() throws SequenceCycleException {
for (int i = 0; i < 31; ++i) {
state = prbs31(state);
if (state == SEED) {
throw new SequenceCycleException();
}
}
return state;
}
This would generate a randomly-seeming sequence of integers that is (231-1)/31 long
DISCLAIMER: naive usage of a single LFSR is highly predictable. In this case, knowing one value gives knowledge of all future values - which makes this method good for simulations (such as games) but also very bad for anything with cryptographic or secret significance!
Personally, I use this method to generate unique IDs for objects that are used as keys in a hash table.