UNPKG

prng-lfsr

Version:

seedable, seeded, pseudo random number generator, prng, linear feedback shift register, fibonnaci, galois, lfsr

53 lines (39 loc) 1.41 kB
// TODO (re)move this guard or find an alternative in the aether var guardSEED16 = function (uint16) { return typeof uint16 === 'number' && uint16 > 0 && uint16 <= 0xffff; } ; // taps at 16, 14, 13, 11 numbered left to right! // |01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16| // |OP| T T T T | var fibonacci16 = function fibonacci16Factory(state) { if (!guardSEED16(state)) return false; return function fibonacci16Next() { var outputBit = ((((state >>> 0) ^ (state >>> 2)) ^ (state >>> 3)) ^ (state >>> 5)) & 1; state = (outputBit << 15) | (state >> 1); // NB. outputBit of this state is: (state >> 15) & 1 return state; }; }; // taps at 16, 14, 13, 11 numbered right to left! // |16|15|14|13|12|11|10|09|08|07|06|05|04|03|02|01| // |T T T T |OP| // |1 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 | // | 11 = B | 4 = 4 | 0 = 0 | 0 = 0 | var galois16 = function galois16Factory(state) { if (!guardSEED16(state)) return false; return function galois16Next() { state = (state >> 1) ^ (-(state & 1) & 0xB400); // NB. outputBit of this state is: (state & 1) return state; }; }; module.exports = { fibonacci16: fibonacci16, galois16: galois16 };