Why would the following "swap" action fail at random times?
int i,p,a[11] = {0,1,2,3,4,5,6,7,8,9,10 };
srand(time(0));
for (i=0;i<11;i++)
{
p = rand() % 11;
a[i] = a[i] ^ a[p];
a[p] = a[i] ^ a[p];
a[i] = a[i] ^ a[p];
}
It is not so different from the the logic in this answer
It will work for a 3/4 runs and then start to duplicate 0
Tried it in C and C++, same results
[edit]
Solved by initializing p=0
and replacing the relevant line with while (p==i) p = rand() % 11;
Update: The reason NOT to use xor
(see Mark Byers' answer and comment)