Exercise 5.3.3

Suppose that instead of swapping element A[i]A[i] with a random element from the subarray A[in]A[i \ldots n], we swapped it with a random element from anywhere in the array:

PERMUTE-WITH-ALL(A)
n = A.length
for i = 1 to n
    swap A[i] with A[RANDOM(1,n)]

Does this code produce a uniform random permutation? Why or why not?

It does not. Intuitivelly, this one can go in nnn^n different ways while there are n!n! combinations. Since n!n! does not divide nnn^n, there is no way that this can be a uniform distribution. (Why doesn't it divide nnn^n? That's the intuitive part. n!n! is divisable by n1n-1, but nnn^n can't be for n>2n > 2).

Of course, this is a popular problem and there are tons of posts and papers written on it. Here's one from Coding Horror