TAP_MASKS = {(10): 576, (16): 46080, (32): 3758096896}

def MaxLengthLFSR(next_val, reglen, max_value=None):
    """Iterate values from seed in max-length LFSR using Galois configuration."""
if max_value is not None:
    assert max_value > 0
while True:
    if max_value is None or next_val < max_value:
        yield next_val
lsb = next_val & 1
next_val = next_val >> 1
if lsb == 1:
    mask = TAP_MASKS[reglen]
next_val ^= mask
sample_count = 5
num_retries = 3
for trynum in range(num_retries):
    it = MaxLengthLFSR(1, 16, 2000)
seq = []
for x in range(sample_count):
    seq.append(next(it))
seq = [str(x) for x in seq]
print('try %s: %s...' % (trynum + 1, ', '.join(seq)))
How to generate a predictable shuffling of a sequence without generating the whole sequence in advance?