title: Random Permutation Generation
body: <p>I am having a hard time understand this code example out of my text book. I hope someone can better explain a part of this code then my book. I am sorry for the lack of comments in this program so I will try my best to be very specific in what I am asking. Ok has the title suggest this program is a "Random Permutation Generation". Where I get confused in the code is in the function called <code>bldPerm()</code> </p>
  
  <p>Function bldPerm():</p>
  
  <pre><code>    void bldPerm(int randNos[])
  {
      int oneRandno;
      int haveRand[ARY_SIZE] = { 0 };
  
      for (int i = 0 ; i &lt; ARY_SIZE; i++)
      {
          do
          {
              oneRandno = rand() % ARY_SIZE;
          } while (haveRand[oneRandno] == 1);
          haveRand[oneRandno] = 1;
          randNos[i] = oneRandno;
      }
      return;
  }
  </code></pre>
  
  <p>I don't understand how and why the do while loop <code>while(haveRand[oneRandno]) == 1;</code> checks the array by comparing it to 1. Then what confuses me is that <code>haveRand[oneRandno] = 1;</code> is set to one in that element. Then that element being "1" is set to <code>randNos[i] = oneRandno;</code> and returns other numbers other then 1. The program works out of my book and does print out other numbers then 1 yet I just can't see how it works. I have been trying to warp my head around this what I am sure is a simple thing... yet I don't get it. So my question is can anyone explain what is going on in this function in detail and how it works? </p>
  
  <p>FULL CODE:</p>
  
  <pre><code>#define _CRT_SECURE_NO_WARNINGS
  #define ARY_SIZE 20
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void bldPerm(int randNos[]);
  void printData(int data[], int size, int linesSize);
  
  int main(void)
  {
      int randNos[ARY_SIZE];
  
      printf("Begin Random Permutation Generation\n");
      bldPerm(randNos);
      printData(randNos, ARY_SIZE, 10);
  
      return 0;
  }
  
  
  void bldPerm(int randNos[])
  {
      int oneRandno;
      int haveRand[ARY_SIZE] = { 0 };
  
      for (int i = 0 ; i &lt; ARY_SIZE; i++)
      {
          do
          {
              oneRandno = rand() % ARY_SIZE;
          } while (haveRand[oneRandno] == 1);
          haveRand[oneRandno] = 1;
          randNos[i] = oneRandno;
      }
      return;
  }
  void printData(int data[], int size, int linesSize)
  {
      int numPrinted = 0;
  
      printf("\n");
      for (int i = 0; i &lt; size; i++)
      {
          numPrinted++;
          printf("%2d ", data[i]);
          if (numPrinted &gt;= linesSize)
          {
              printf("\n");
              numPrinted = 0;
          }
      }
      printf("\n");
      return;
  }
  </code></pre>