Usually a random permutation for an array with n elements means a uniform distribution from n!
possibilities, and the Knuth shuffle is used to do so:
for i from n − 1 downto 1 do
j ← random integer with 0 ≤ j ≤ i
exchange a[j] and a[i]
But with the constraint that a[i] != i
, I have no idea how to form such a permutation uniformly.
For example, with n = 3, how to form a permutation randomly from the possibilities below?
{1, 2, 0}, {2, 0, 1}