Perhaps this is better suited to a cryptography stack exchange, but I thought I'd try in mathematics in case this question is more obvious than I initially thought.
I'm looking for a function $~f:\{1,2,...,n\} \to \{1,2,...,n\}$ that satisfies the following criteria:
- $f$ is invertible (and ideally, relatively easy to invert, computationally speaking).
- $f$ has the appearance of mapping inputs in a uniform random manner to its range. That is, for $n = 5$ we might have $~f(1) = 4,~f(2) = 2,~f(3) = 1,~f(4) = 5,~f(5) = 3$. Of course, it just needs to appear random since the function must also be deterministically invertible. In fact, any sufficiently "messy" output would satisfy the needs for $f$.
- $f$ should be defined for arbitrary $n$
Maybe this is a well documented problem and I just lack the proper vocabulary to do a quick search...
Thank you!
Many linear congruential PRNG can be used. Take $a$ with $\gcd(a,n)=1,\;$ i.e. $a^{-1} \bmod n$ exists and define $$f(x) = 1 + ((ax+b) \bmod n),\;$$ where $b$ is any integer. Then $f$ maps $[1,n]$ to $[1,n]$ and is invertible: from $y=f(x)$ you compute $$x=a^{-1}(y-1-b)\bmod n,$$ and if $x=0$ you should set $x=n,\,$ this comes from your unusal domain (normally $[0,n-1]$ is used).