title: Fill a table preserving rules
body: <p>My input is a table in the following the format:</p>
  
  <pre><code>_input = {
      ["Item1"] = {
          min = 1,
          max = 1,            
          pos = {
              [1] = nil,
              [2] = {--[[somedata]]},
              [3] = nil,
              [4] = {--[[somedata]]},
              [5] = nil,
              [6] = {--[[somedata]]},
              [7] = nil,
              [8] = {--[[somedata]]},
          },
      },
      ["Item2"] = {
          min = 1,
          max = 1,
          pos = {
              [1] = nil,
              [2] = nil,
              [3] = nil,
              [4] = {--[[somedata]]},
              [5] = {--[[somedata]]},
              [6] = {--[[somedata]]},
              [7] = nil,
              [8] = nil,
          },
      },
      ["Item3"] = {
          min = 1,
          max = 2,
          pos = {
              [1] = nil,
              [2] = {--[[somedata]]},
              [3] = nil,
              [4] = {--[[somedata]]},
              [5] = {--[[somedata]]},
              [6] = {--[[somedata]]},
              [7] = nil,
              [8] = nil,
          },
      },
      ["Item4"] = {
          min = 1,
          max = 3,
          pos = {
              [1] = {--[[somedata]]},
              [2] = {--[[somedata]]},
              [3] = {--[[somedata]]},
              [4] = nil,
              [5] = nil,
              [6] = nil,
              [7] = {--[[somedata]]},
              [8] = {--[[somedata]]},
          },
      },
  }
  </code></pre>
  
  <p>Each entry in <code>_input</code> has the fields <code>min</code>, <code>max</code> and <code>pos</code>, while <code>pos</code> itself contains eight entries, either <code>nil</code> or filled with data.
  There are not always four items given in <code>_input</code>. There can be more items or less items.</p>
  
  <p>My goal is to create an algorithm that produces a single table, filled with appropriate values from <code>_input</code> and preserving the <code>min</code>/<code>max</code> rules (that is: the minimum/maximum amount of data-items from <code>pos</code> in the final table. There have to be <code>min</code> items in the final output and there can be <code>max</code> items in the final output).</p>
  
  <p>Given the input above, the output may look like this:</p>
  
  <pre><code>_output = {
      [1] = {
          type = "Item4",
          data = {--[[the data from _input["Item4"].pos[1] ]]},
      },
      [2] = {
          type = "Item1",
          data = {--[[the data from _input["Item1"].pos[2] ]]},
      },
      [3] = {
          type = "Item4",
          data = {--[[the data from _input["Item4"].pos[3] ]]},
      },
      [4] = {
          type = "Item3",
          data = {--[[the data from _input["Item3"].pos[4] ]]},
      },
      [5] = nil,
      [6] = {
          type = "Item2",
          data = {--[[the data from _input["Item2"].pos[6] ]]},
      },
      [7] = {
          type = "Item4",
          data = {--[[the data from _input["Item4"].pos[7] ]]},
      },
      [8] = nil,
  }
  </code></pre>
  
  <p>Not each field in the output has to be filled:<br>
  <code>5</code> and <code>8</code> are nil in the above example.<br>
  <code>5</code> can't be filled, because the only possible items would be <code>Item2</code> and <code>Item3</code>. <code>Item2</code> already reached the maximum amount and <code>Item3</code> doesn't have to reach the maximum amount.<br>
  <code>8</code> can't be filled, because the possible items <code>Item1</code> and <code>Item4</code> both already reached their maximum amount.</p>
  
  <p>This is my approach so far, but it does not preserve all the rules and produces "wrong" output. Furthermore I would love to not get the same results from the same input everytime.</p>
  
  <pre><code>local _output = {
      [1] = nil,
      [2] = nil,
      [3] = nil,
      [4] = nil,
      [5] = nil,
      [6] = nil,
      [7] = nil,
      [8] = nil,
  }
  for key in pairs(_input) do
      local _item = _input[key]
  
      for i=0,math.random(_item.min, _item.max),1 do
          -- I omit deepCopy() for readability
          local _possibleCopy = deepCopy(_item.pos)
  
          for i=1,8,1 do
              if _output[i] ~= nil then
                  _possibleCopy[i] = nil
              end
          end
  
          local _possibleSlots = {}
  
          for i=1,8,1 do
              if _possibleCopy[i] ~= nil then
                  _possibleSlots[#_possibleSlots+1] = i
              end
          end
  
          local _slot = _possibleSlots[math.random(1,#_possibleSlots)]
  
          if _slot then
              _output[_slot] = {
                  type = key,
                  data = _item.pos[_slot],
              }
          end
      end
  end
  </code></pre>