#### FactChecker

Science Advisor

Gold Member

2018 Award

- 4,661

- 1,588

It is notoriously difficult to design an algorithm for a random number generator. Anything that one thinks will be random is very likely to display patterns that are clear to see when looked at the right way. Your program looks like an example of that. A classic book which contains a lot on the subject is Knuth, "Art of Computer Programming, Volume 2: Seminumerical Algorithms" .Another example of a generator that will (eventually, and very very very slowly) not repeat is this one, for say 8-bit integers (or whatever size integers we choose):

that algorithm (a brute-force search, actually) will eventually generate a non-repeating, whatever-length sequence that will pass whatever randomness criteria we set... at a cost of taking ages to process... hmm... actually this is probably the slowest random number generator ever... I should get a prize or something for thatCode:`initialize a[i] = 0 for i in 1..N repeat calculate randomness of a[] for i = 1..N, for j = 1 to 8: invert bit j of a[i] recalculate randomness of a[] if new sequence has lower randomness, revert the bit back until randomness of a[] > desired-target`