Ejemplo n.º 1
0
 private static int[] ModifySequenceToTotal(CryptoRandom rnd,
     int[] sequences,
     int minValue,
     int maxValue,
     int total)
 {
     var sum = sequences.Sum();
     var runs = Math.Abs(total - sum);
     if (runs > 0)
     {
         sequences = sequences.OrderBy(s => rnd.Next()).ToArray();
         var doIncrease = sum < total;
         var currentIndex = 0;
         for (var i = 0; i < runs;)
         {
             var currentValue = sequences[currentIndex];
             if (doIncrease && currentValue + 1 <= maxValue || !doIncrease && currentValue - 1 >= minValue)
             {
                 sequences[currentIndex] = currentValue + (doIncrease ? 1 : -1);
                 i++;
             }
             currentIndex++;
             if (currentIndex >= sequences.Length)
             {
                 currentIndex = 0;
             }
         }
     }
     return sequences;
 }