@jancsika After spending much time with this question, I think I finally figured out what is happening. The good news is that indeed there are absolutely no problems at all with your algorithm, and it is probably the best way to generate such sequences of non-repeating integer numbers.
At first sight, your algorithm seems to take as input random integers ranging between 0
and 2
(i.e. 3 values) and output random integers ranging between 0
and 3
(i.e. 4 values) for each cycle, which seems to be problematic due to upsampling (I explained what this means above). But actually the algorithm is always choosing among 3 options only, given that each value cannot be the same as the previous one. For instance, if the very first random integer selected is 0
, there are three possible values for the next integer (1
, 2
or 3
), which is exactly what the range PRGN is providing. So the key is to realize that 3 random values are being mapped into 4 non-repeating random values, and this can be done without causing any unwanted patterns.
Therefore, there is no problem using MOD N+1
for a random input ranging from 0
to N
, because the amount of information does not change with that. But when we use MOD N+2
or larger, we actually do observe patterns that shouldn't be there if the output was truly random. For instance, certain sequences of two consecutive numbers never appear: e.g. taking N = 3
(i.e. input between 0
and 2
) and MOD 5
, one will never see a 0
followed by a 4
, since there is no input such that the expression ((input + 1) + 0) MOD 5 = 4
would be true.
Anyway, I hope this clear something up a bit. And now I finally can sleep at night
Cheers!
Gilberto