Forgive the quirkiness of the title; I am not aware of a name for this concept, therefore I just made a few up.
This blogpost is about a particular way to construct what I call a "pseudorandom permuter". It is distinct from a regular pseudorandom number generator (PRNG) in that it is constructed such that the sequence will never repeat until all $$$M = 2^k$$$ integers in its machine-integer state space (32- or 64-bit) has been used. In other words, it's a way to produce a permutation of $$$[0, M)$$$ that's (hopefully) statistically indistinguishable from a truly random permutation.
The generator is made up of two parts, a "discrete Weyl sequence", and an "avalanching perfect hash function".
A "discrete Weyl sequence" is defined by a parameterized function $$$W_{s, \gamma}: [0, M) \rightarrow [0, M)$$$ where the $$$i$$$-th member of the sequence is $$$W_{s, \gamma}(i) = (s + \gamma \cdot i) \mod M$$$. The two parameters can be chosen using a standard random generator, with the caveat that $$$\gamma$$$ must be odd (this can be easily ensured by a | 1
instruction). This ensures it is coprime with the machine integer size $$$M$$$.
The advantage of the discrete Weyl sequence is that as it is parameterized, it is hard to predict by an adversarial test (whether by the problemsetter or by the "hacking" mechanic). The disadvantage however, is that it is extremely regular, and this regularity may be exploited by adversarial tests.
In comes the second part. An "avalanching perfect hash function" is a function that is "perfect" in the sense that it maps $$$[0, M) \rightarrow [0, M)$$$ without collisions, i.e. $$$a \neq b \Leftrightarrow f(a) \neq f(b)$$$, and exhibits the avalanche property, which is that flipping one bit in the input should flip roughly half the bits of the output without a regularly predictable pattern. Note that our discrete Weyl sequence fits the definition of a perfect hash function, however it doesn't exhibit an avalanche property.
Constructing a good APHF is more difficult, however since it doesn't have to be parameterized, we can simply use a fixed function. One option is to use the "multiply-xorshift" construction from the "splitmix64" generator:
function splitmix64_aphf(z: uint64): uint64 {
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
}
where ^
denotes the binary xor operator, and >>
the unsigned right-shift operator. I also use another option for 32-bit integers, taken from this offsite blogpost:
function int32_aphf(z: uint32): uint32 {
z = (z ^ (z >> 16)) * 0x7feb352d;
z = (z ^ (z >> 15)) * 0x846ca68b;
return z ^ (z >> 16);
}