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