I have one int*
array and I want to perform a random permutation to this array. The function is similar with randperm
in MATLAB. For example, I have an array int* A=[0 1 1 0]
and if I call randperm(A,sizeof A)
then one possible output would be A=[1 0 0 1]
This is my function but there seems to be an error. Can anyone determine what this error is?
void randperm(int* permMatrix,int n)
{
int i, j, t;
for(i=0; i<n; i++) {
j = rand()%(n-i)+i;
t = permMatrix[j];
permMatrix[j] = permMatrix[i];
permMatrix[i] = t;
}
for(i=0; i<n; i++) {
printf("%d ",permMatrix[i]);
}
}