views:

30

answers:

1

If I have an event that happens 15 times a second (numbered 1 - 15), but I only want to process it 3 times I can choose [1], [6] and [11],. It's important that the events I process are as evenly spaced as possible and take into account wrap-around, i.e. the events are continuous 13, 14, 15, 1, 2, 3 etc.

If I want 4 items the best I can do is [1], [5], [9] & [13].

Is there a general algorithm that will calculate which events I need to process given the total number of events (total) and the number to process (processAmount).

+1  A: 

You can try to use this basic approach - divide your interval to the equal non-integer parts and take the closest integer event index for each event:

int gaps = total+1;
double step = 1.0*gaps/processAmount;

for (int i = 0; i < processAmount;++i)
   int index = (int)(1 + i*step);
   //output or handle index
}
Vladimir
Perfect, thank you!!
Matt Warren