Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/28/14 11:38:13 (10 years ago)
Author:
abeham
Message:

Fixed bug in sample proportional without repetition

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Random/3.3/RandomEnumerable.cs

    r9456 r10407  
    199199      HashSet<int> chosenIndices = new HashSet<int>();
    200200      while (chosenIndices.Count < sourceArray.Length) {
    201         int index = 0;
    202         double ball = valueArray[index], sum = random.NextDouble() * total;
     201        int index = -1;
     202        double ball = 0, sum = random.NextDouble() * total;
    203203        while (ball < sum) {
    204204          index++;
    205205          if (!chosenIndices.Contains(index))
    206             ball += valueArray[++index];
    207         }
     206            ball += valueArray[index];
     207        }
     208        index = index >= 0 ? index : Enumerable.Range(0, sourceArray.Length).First(x => !chosenIndices.Contains(x));
    208209        yield return sourceArray[index];
    209210        chosenIndices.Add(index);
Note: See TracChangeset for help on using the changeset viewer.