title: How to distribute items evenly, without random numbers
body: <p>I have a situation where I need to evenly distribute N items across M slots. Each item has its own distribution %. For discussion purposes say there are three items (a,b,c) with respective percentages of (50,25,25) to be distributed evenly across 20 slots. Hence 10 X a,5 X b &amp; 5 X c need to be distributed. The outcome would be as follows:</p>
  
  <pre><code> 1. a
   2. a
   3. c
   4. b
   5. a
   6. a
   7. c
   8. b
   9. a
   10. a
   11. c
   12. b
   13. a
   14. a
   15. c
   16. b
   17. a
   18. a
   19. c
   20. b
  </code></pre>
  
  <p>The part that I am struggling with is that the number of slots, number of items and percentages can all vary, of course the percentage would always total up to 100%. The code that I wrote resulted in following output, which is always back weighted in favour of item with highest percentage. Any ideas would be great.</p>
  
  <pre><code> 1. a
   2. b
   3. c
   4. a
   5. b
   6. c
   7. a
   8. b
   9. c
   10. a
   11. c
   12. b
   13. a
   14. b
   15. c
   16. a
   17. a
   18. a
   19. a
   20. a
  </code></pre>
  
  <p><strong>Edit</strong>
  This is what my code currently looks like. Results in back weighted distribution as I mentioned earlier. For a little context, I am trying to evenly assign commercials across programs. Hence every run with same inputs has to result in exactly the same output. This is what rules out the use of random numbers.</p>
  
  <pre><code>foreach (ListRecord spl in lstRecords){
  
      string key = spl.AdvertiserName + spl.ContractNumber + spl.AgencyAssignmentCode;
  
      if (!dictCodesheets.ContainsKey(key)){
  
          int maxAssignmentForCurrentContract = weeklyList.Count(c =&gt; (c.AdvertiserName == spl.AdvertiserName) &amp;&amp; (c.AgencyAssignmentCode == spl.AgencyAssignmentCode)
                                                 &amp;&amp; (c.ContractNumber == spl.ContractNumber) &amp;&amp; (c.WeekOf == spl.WeekOf));
  
  
          int tmpAssignmentCount = 0;
  
          for (int i = 0; i &lt; tmpLstGridData.Count; i++)
          {
              GridData gData = tmpLstGridData[i];
  
              RotationCalculation commIDRotationCalc = new RotationCalculation();
              commIDRotationCalc.commercialID = gData.commercialID;
  
              commIDRotationCalc.maxAllowed = (int)Math.Round(((double)(maxAssignmentForCurrentContract * gData.rotationPercentage) / 100), MidpointRounding.AwayFromZero);
              tmpAssignmentCount += commIDRotationCalc.maxAllowed;
  
              if (tmpAssignmentCount &gt; maxAssignmentForCurrentContract)
              {
                  commIDRotationCalc.maxAllowed -= 1;
              }
  
  
              if (i == 0)
              {
                  commIDRotationCalc.maxAllowed -= 1;
                  gridData = gData;
              }
  
              commIDRotationCalc.frequency = (int)Math.Round((double)(100/gData.rotationPercentage));
  
              if (i == 1)
              {
                  commIDRotationCalc.isNextToBeAssigned = true;
              }
  
              lstCommIDRotCalc.Add(commIDRotationCalc);
          }
  
          dictCodesheets.Add(key, lstCommIDRotCalc);
  
      }else{
  
              List&lt;RotationCalculation&gt; lstRotCalc = dictCodesheets[key];
  
              for (int i = 0; i &lt; lstRotCalc.Count; i++)
              {
  
                  if (lstRotCalc[i].isNextToBeAssigned)
                  {
                      gridData = tmpLstGridData.Where(c =&gt; c.commercialID == lstRotCalc[i].commercialID).FirstOrDefault();
                      lstRotCalc[i].maxAllowed -= 1;
  
                      if (lstRotCalc.Count != 1)
                      {
                          if (i == lstRotCalc.Count - 1 &amp;&amp; lstRotCalc[0].maxAllowed &gt; 0)
                          {
                              //Debug.Print("In IF");
                              lstRotCalc[0].isNextToBeAssigned = true;
                              lstRotCalc[i].isNextToBeAssigned = false;
  
                              if (lstRotCalc[i].maxAllowed == 0)
                              {
                                  lstRotCalc.RemoveAt(i);
                              }
  
                              break;
                          }
                          else
                          {
                              if (lstRotCalc[i + 1].maxAllowed &gt; 0)
                              {
                                  //Debug.Print("In ELSE");
                                  lstRotCalc[i + 1].isNextToBeAssigned = true;
                                  lstRotCalc[i].isNextToBeAssigned = false;
  
                                  if (lstRotCalc[i].maxAllowed == 0)
                                  {
                                      lstRotCalc.RemoveAt(i);
                                  }
  
                                  break;
                              }
                          }
                      }
  
                  }
              }
  
          }
  
  }
  </code></pre>
  
  <p><strong>Edit 2</strong>
  Trying to clear up my requirement here. Currently, because item 'a' is to be assigned 10 times which is the highest among all three items, towards the end of distribution, items 16 - 20 all have been assigned only 'a'. As has been asked in comments, I am trying to achieve a distribution that "looks" more even.</p>