Variable Period PRNG

I need to create a pseudo random number generator in place with an adjustable period. In addition, during one period there should be no collisions. That is, the following should return true:

// prng is "generated" at run-time
// (though a by-hand solution would work)

bool test(func prng, int period) {
    int seed = 0;  // Any number should work
    int cur = seed;

    for (int i = 0; i <= period; ++i) {
        cur = prng(cur);

        if (cur == seed) {
            if (i == period) {
                // We hit our period on target
                return true;
            }

            // Period too low (we hit our seed already!)
            return false;
        }
    }

    // Period too high
    return false;
}

(For example, pseudocode, the answer in any public language (C ++, Python, Haskell, etc.) is acceptable.)

PRNG should not depend on a variable static state when generating numbers. That is, I cannot have a large table of already returned numbers or anything like that. It should rely only on a given input to generate the next term.

The algorithm should not be cryptographically strong (of course) or "strongly" random. However x % periodunacceptable; it should be at least somewhat random.

, , -, .

(- - , ( ).)

+3
1

, (LFSR).

Wikipedia.

:

The initial value of the LFSR is called the seed, and because the operation 
of the register is deterministic, the stream of values produced by the 
register is completely determined by its current (or previous) state. 
Likewise, because the register has a finite number of possible states, it must 
eventually enter a repeating cycle. However, an LFSR with a well-chosen 
feedback function can produce a sequence of bits which appears random and 
which has a very long cycle.

, LFSR 2 N -1.
, , P N, "" 2 N -1, 2 (N-1) - 1 ( , , N-1).

, k (k = ((2 N -1) - P) ⋳ {1..., 2 (N-1) - 1}), , ​​


    If (Mod(cur,2(N-1)+1-k) == 0) Then
       cur=Mod(cur+1,2N-1)
+2

Source: https://habr.com/ru/post/1761657/


All Articles