title: Random number for javascript/jquery form
body: <p>Okay, so I have the following form fields:</p>
  
  <pre><code>1. [] [] [] x
  2. [] [] [] x
  3. [] [] [] x
  4. [] [] [] x
  </code></pre>
  
  <p>[rand]</p>
  
  <p>I allow the user to insert random numbers into the forms by clicking "rand":</p>
  
  <pre><code>1. [01] [09] [12] x
  2. [03] [12] [13] x
  3. [12] [15] [16] x
  4. [01] [02] [03] x
  </code></pre>
  
  <p>[rand]</p>
  
  <p>If the user so desires, they can delete a row and add a new one</p>
  
  <pre><code>1. [01] [09] [12] x
  3. [12] [15] [16] x
  4. [01] [02] [03] x
  5. [] [] []  x
  </code></pre>
  
  <p>[rand]</p>
  
  <p>Each row is named by its fields:</p>
  
  <pre><code>1. [01] [09] [12] = name=row_name[1][0], row_name[1][0], row_name[1][0]
  3. [12] [15] [16] = name=row_name[3][0], row_name[3][0], row_name[3][0]
  4. [01] [02] [03] = name=row_name[4][0], row_name[4][0], row_name[4][0]
  5. [] [] []       = name=row_name[5][0], row_name[5][0], row_name[5][0]
  </code></pre>
  
  <p>[rand]</p>
  
  <p>The problem I face is when a user deletes a number of rows, the sequences are now out of order, so instead of: 1,2,3,4 I have: 1,3,4,5 </p>
  
  <p>This means my random function will not work on row 5 since it is looking for 1,2,3,4 (in a for loop).</p>
  
  <p><strong>So my dilemma is how best to code this...</strong></p>
  
  <p>On the one hand, I am thinking of re-naming the fields each time a user deletes a row to keep them in sequence: 1,2,3,4</p>
  
  <p>On the other hand I am thinking that I could just ignore the missing fields and improve how I insert the numbers.</p>
  
  <p><strong>The rand function works as follows:</strong></p>
  
  <pre><code>var numbers_per_row = 3;
  var total_numbers = 4;
  var rows = getRandomNumbers(numbers_per_row,total_numbers);
  
  This returns an array: [
     0 =&gt; [01,09,12],
     1 =&gt; [03,12,13],
     2 =&gt; [12,15,16],
     3 =&gt; [01,02,03]
  ]
  </code></pre>
  
  <p>Now that I have the array, I next need to insert the random numbers into the form fields:</p>
  
  <pre><code>var total_rows = rows.length();
  
  for (var 1 = 0; i &lt; total_rows; i++) {
  
      var line = row[i];
      for (var x =0; x &lt; numbers_per_row; x++) {
          var field_name = 'row_name['+(i+1)+']['+x+'];
          $("#formName input[ name='" + field_main + "' ]").val(line[x]);
      }
  
  }
  </code></pre>
  
  <p>Unfortunately this function does not take into account the missing values that were deleted.</p>
  
  <p><strong>I have been pondering the most logical way to go about this. Any ideas?</strong></p>