title: Generate valid sudoku puzzle in C
body: <p>I try to generate a valid sudoku puzzle in C, when i compile, nothing happend, i think there is an infinite loop but I don't know where it can come from! I think I need help! In my reasoning, i try to :
  Randomly take any number 1-9.
  Check if it is safe to put in the cell.(row , column and box)
  If safe, place it and increment to next location and go to step 1.
  If not safe, then without incrementing go to step 1.
  Once matrix is fully filled, remove k no. of elements randomly to complete game.
  Here my code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;limits.h&gt;
  #include &lt;time.h&gt;
  #include &lt;math.h&gt;
  
  #define N 9 // row and col 
  #define SRN 3 // square and row
  #define K 20 //missing digits 
  
  int mat[N][N];
  
  bool unUsedInBox(int rowStart, int colStart, int num) {
    for (int i = 0; i &lt; SRN; i++)
      for (int j = 0; j &lt; SRN; j++)
        if (mat[rowStart + i][colStart + j] == num)
          return false;
  
    return true;
  }
  
  int randomGenerator(int num) {
    srand(time(NULL));
    return (int) ( rand() % num ) + 1;
  }
  
  bool unUsedInRow(int i, int num) {
    for (int j = 0; j &lt; N; j++)
      if (mat[i][j] == num)
        return false;
    return true;
  }
  
  // check in the row for existence 
  bool unUsedInCol(int j, int num) {
    for (int i = 0; i &lt; N; i++)
      if (mat[i][j] == num)
        return false;
    return true;
  }
  
  bool CheckIfSafe(int i, int j, int num) {
    return (unUsedInRow(i, num) &amp;&amp;
      unUsedInCol(j, num) &amp;&amp;
      unUsedInBox(i - i % SRN, j - j % SRN, num));
  }
  
  void fillBox(int row, int col) {
    int num;
    for (int i = 0; i &lt; SRN; i++) {
      for (int j = 0; j &lt; SRN; j++) {
        do {
          num = randomGenerator(N);
        }
        while (!unUsedInBox(row, col, num));
  
        mat[row + i][col + j] = num;
      }
    }
  }
  
  void fillDiagonal() {
    for (int i = 0; i &lt; N; i = i + SRN)
      fillBox(i, i);
  }
  
  bool fillRemaining(int i, int j) {
    if (j &gt;= N &amp;&amp; i &lt; N - 1) {
      i = i + 1;
      j = 0;
    }
    if (i &gt;= N &amp;&amp; j &gt;= N)
      return true;
  
    if (i &lt; SRN) {
      if (j &lt; SRN)
        j = SRN;
    } else if (i &lt; N - SRN) {
      if (j == (int)(i / SRN) * SRN)
        j = j + SRN;
    } else {
      if (j == N - SRN) {
        i = i + 1;
        j = 0;
        if (i &gt;= N)
          return true;
      }
    }
  
    for (int num = 1; num &lt;= N; num++) {
      if (CheckIfSafe(i, j, num)) {
        mat[i][j] = num;
        if (fillRemaining(i, j + 1))
          return true;
  
        mat[i][j] = 0;
      }
    }
    return false;
  }
  
  void removeKDigits() {
    int count = K;
    while (count != 0) {
      int cellId = randomGenerator(N * N);
  
      int i = (cellId / N);
      int j = cellId % 9;
      if (j != 0)
        j = j - 1;
  
      if (mat[i][j] != 0) {
        count--;
        mat[i][j] = 0;
      }
    }
  }
  
  void fillValues() {
    // Fill the diagonal of SRN x SRN matrices 
    fillDiagonal();
  
    // Fill remaining blocks 
    fillRemaining(0, SRN);
  
    // Remove Randomly K digits to make game 
    removeKDigits();
  }
  
  void printSudoku() {
    for (int i = 0; i &lt; N; i++) {
      for (int j = 0; j &lt; N; j++){
          printf("%d",mat[i][j]);
          printf(" ");
      }
        printf("\n");
    }
    printf("\n");
  }
  
  int main() {
    fillValues();
    printSudoku();
  }
  </code></pre>