// TODO: find more triplets for different n sclass TripletLSFR { // params int n; int start = 1; int a, b, c; // the triplet // changing variables int step, value; // triplets table for n's from 1 to 31 // (just the first triple that gives a full cycle, including degenerate ones, // so not optimized for "randomness") static final int[][] triplesTable = { { 0, 0, 0 }, { 0, 1, 1 }, { 0, 1, 1 }, { 0, 1, 3 }, { 0, 1, 1 }, { 0, 1, 1 }, { 0, 1, 3 }, { 1, 1, 2 }, { 0, 1, 1 }, { 0, 3, 7 }, { 0, 1, 1 }, { 1, 1, 4 }, { 0, 1, 6 }, { 0, 1, 1 }, { 0, 1, 4 }, { 1, 1, 14 }, { 0, 3, 7 }, { 1, 3, 14 }, { 0, 1, 3 }, { 1, 5, 6 }, { 0, 1, 6 }, { 0, 1, 21 }, { 0, 1, 1 }, { 1, 5, 18 }, { 0, 1, 12 }, { 0, 1, 1 }, { 0, 2, 7 }, { 0, 3, 25 }, { 0, 1, 1 }, { 0, 1, 1 }, { 0, 3, 28 } }; *(int *n, int a, int b, int c) { value = start; // handle triplets with 0s (meaning to skip one of the 3 xorshifts) meta-for a in a, b, c { this.a = a == 0 ? n : a; } } int cycleLength() { ret (1 << n)-1; } int next() { int mask = ((1 << n)-1); step = (step+1) & mask; int x = value; x ^= (x << a) & mask; x ^= x >>> b; x ^= (x << c) & mask; ret value = x; } }