## Non-uniform shuffling

A shuffle of a finite sequence of length $n$ of distinguishable elements refers to an algorithmic process which — modulo pseudo-randomness — can be modeled as a random variable uniformly distributed on the permutations $\mathbb{S}_n$.
However, most pseudo-random entropy sources provide only a pseudo-uniformly distributed realization of $\mathbb{F}_2^\mathbb{N}$, leading to the necessity of finding an algorithmic transformation process if one wishes to achieve a shuffle.
In the following, I will assume that a transforming process to a family of independent and uniformly on $\{1..n\}$ distributed random variables is already present for any $n\in\mathbb{N}$.

One naive and seemingly correct (it is not) approach is to traverse the given sequence, uniformly swapping the current entry with another one, i.e.

void falseShuffle(uint64_t *arr, size_t len) {
for (size_t j = 0; j < len; j++)
swap(arr, j, unif(len)); }

as an exemplary C implementation where $\texttt{unif}(n)$ is independent and uniformly distributed on $\{0..n-1\}$.

Yet, even though sensible on first sight, the above defined random variable is only in the most trivial cases uniformly distributed and — as empirical evidence suggests, see below — horrendously non-uniformly distributed otherwise.
To prove the non-uniformity postulated above, I first present the following number-theoretic result.

Claim. In only three trivial cases does the factorial of a natural number divide its tetration; formally

$\forall\,n\in\mathbb{N}_{>2}:n!\nmid n^n$.

Proof. Let $n\in\mathbb{N}_{>2}$ be a natural number larger than two. By the definition of the factorial, $\prod_{p is evident. Adhering to the uniqueness of prime factorizations, $\prod_{p follows. Observe that $n-1>1$ has to be prime since $\forall\,p, implying $n-1\mid\prod_p=n$ which cannot hold for $n>2$. QED

Now suppose, $\texttt{falseShuffle}$ was indeed non-trivially distributed uniformly. Without loss of generality, all involved probability spaces were finite. Then there had to exist a surjection from this algorithm’s entropic state to $\mathbb{S}_n$ with fibers of the same finite cardinality, implying $n!\mid n^n$. By the above proven claim, $n<3$ followed, making the distribution trivial. QED

One possible reason for the surprising nature of this non-uniformity is the striking source code resemblance to a correct implementation, i.e.

void shuffle(uint64_t *arr, size_t len) {
for (size_t j = 0; j < len; j++)
swap(arr, j, j + unif(len - j)); }

as an exemplary C implementation which can be inductively shown to resemble the same structure as $\mathbb{S}_n$, in each step sprinkling in some uniform randomness and thus being itself uniformly distributed.

To see just how non-uniform $\texttt{falseShuffle}$ is, I have calculated its discrete density for $n=4$:

[       |                ]
[   |   |||              ]
[   |   |||              ]
[   |   |||              ]
[   ||  ||||||| ||       ]
[||||| |||||||| |||    ||]
[|||||||||||||||||| || ||]
[||||||||||||||||||||||||]
[||||||||||||||||||||||||]
[||||||||||||||||||||||||]
[||||||||||||||||||||||||]
[||||||||||||||||||||||||]
[||||||||||||||||||||||||]
[||||||||||||||||||||||||]
[||||||||||||||||||||||||]
n = 4

If it was uniformly distributed, the discrete density would look like a rectangle; [||||| ... |||||]. Further plots for $0\leq n\leq 6$ are shown in nonUniformity.txt.

Source code for the analysis and plotting: nonUniformity.hs. Empirical evidence of non-uniformity: nonUniformity.c.

## Generating the Prouhet-Thue-Morse sequence in brainfuck

[-]<<<[-]<<<[-]<<<[-]<<<[-]<<<[-]>>>>>>>>>>>>>>>>>[-]++++++++++++++++++++++++++++++++++++++++++++++++.[-]<[-]>>[-]<[-<+>>+<]>[-<+>]<<<<<[-]>>>[<<<+>>>-]<<<<<<<<<<<<[-]>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-]<<<<<<<[<<<<<<<<<<<[-]>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-]<<<<<<<]<[-]<[>+<-]>>+<<<<<<<<<[-][-]>>>>>>>>>[>>>>>>>>>]>>[-]+[>[-]+[<<[-]<<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[-]>>>>>>>[-]<<<<<<<<[>>>>>>>>+<<<<<<<<-]>>>>>>>>>>[>>>>>>>[-]<<<<<<<<<[>>>>>>>>>+<<<<<<<<<-]>>>>>>>>>>>]<<[>>>+<<<-]>>>>>>[-]<<<[->>>+<<<][-]>>>>[-]<[-<<<+>>>>+<]>[-<+>]<<<<<<<<<<[-]>>>>>>[<<<<<<+>>>>>>-]<<<<<<<<<<<<<<<[-]>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-]<<<<<<<[<<<<<<<<<<<[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>>>+<<<<<<<<-]>>>>>>>>>>[>>>>>>>[-]<<<<<<<<<[>>>>>>>>>+<<<<<<<<<-]>>>>>>>>>>>]<<[>>>>>>>>>+<<<<<<<<<-]>>>>>>>>>>>>[-]<<<[->>>+<<<]>>>>[-]++++++++++++++++++++++++++++++++++++++++++++++++>[-]<<[->+>+<<]>>[-<<+>>]<.<<<<[-]>>>>[-]<[-<<<+>>>>+<]>[-<+>]<<<<<<<[-]>>>[<<<+>>>-]<<<<<<<<<<<<[-]>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-]<<<<<<<[<<<<<<<<<<<[-]>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-]<<<<<<<]<[-]<[>+<-]>>+<<<<<<<<<[-][-]>>>>>>>>>[>>>>>>>>>]>[-]<<<<<<<<<<<<<<<<<<[-]>>[<<+>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<-]<<[>>+<<-]>>>>>>>>>>>>>>>>>>>>[-]<<[->>+<<]>>]<]

Try it online.

## Factoids #1

#### IV) commutative, non-associative operations

For any natural number n, let $\mathrm{Op}_n:=\left\{\star:\mathbb{Z}^2_n\to\mathbb{Z}_n\right\}$ denote the set of all operations on a set of that order. An operation  shall be called commutative iff $\mathrm{commut}(\star):\Leftrightarrow\forall\,x,y\in\mathbb{Z}_n:x\star y=y\star x$ and be called associative iff $\mathrm{assoc}(\star):\Leftrightarrow\forall\,x,y,z\in\mathbb{Z}_n:x\star(y\star z)=(x\star y)\star z$ holds.

With the above defined, one may study $\mathrm{CnA}_n:=\{\star\in\mathrm{Op}_n:\mathrm{commut}(\star)\land\lnot \mathrm{assoc}(\star)\}$. For n = 2, this set is nonempty for the first time, containing a manageable two elements, by name

$\mathrm{CnA}_2=\Big\{\mathrm{nor}:(x,y)\mapsto 1+xy, \quad\mathrm{nand}:(x,y)\mapsto (1+x)\cdot(1+y)\Big\}$.

However, based on the superexponential nature of $\#\mathrm{Op}_n=\#\mathbb{Z}_n^{\mathbb{Z}_n^2}=\#\mathbb{Z}_n^{{\#\mathbb{Z}_n}^2}=n^{n^2}$, the sequence $\mathrm{A079195}_n:=\#\mathrm{CnA}_n$ likely also grows rather quickly, OEIS only listing four members;

$\mathrm{A079195}=(0, 2, 666, 1\,047\,436, \dots)$.

Based on this limited numerical evidence, I would suspect the commutative yet non-associative operations to be rather sparse, i.e.

$\lim\limits_{n\to\infty}\mathrm{A079195}_n\cdot\left(\#\mathrm{Op_n}\right)^{-1}=0\mod\square$.

Analysis source: operations.hs

(Non-)commutative and (non-)associative operations have also been studied nearly twenty years ago by Christian van den Bosch, author of OEIS sequence A079195. Unfortunately, their site appears to be down, which is where they hosted Closed binary operations on small sets (resource found on web.archive.org).

#### V) Arbitrary polynomial extremum difference

Let ε > 0 be an arbitrary distance, define $g := -4x^4-x^3+8x^2+3x-4$. Then $f:=\epsilon\cdot 4^{-1}\cdot g$ has two local maxima at -1 and 1, whose vertical distance is ε.

#### VI) Digit sum roots

It holds that $\mathrm{ds}_{10}(108^{12})=108$.