I am given a list of months (TOTAL_NUM_MONTHS) and from this list I need to select only 12 months proportionally. I have following algorithm up to now:
IF TOTAL_NUM_MONTHS<12 THEN
STEP = 1
ELSE IF (TOTAL_NUM_MONTHS % 12 == 0) THEN
STEP = TOTAL_NUM_MONTHS / 12
ELSE
STEP = ???
So, if total number of months is less then 12 I will select all of them, and if it is divisible to 12 then I will select them with a step equal to TOTAL_NUM_MONTHS/12.
Question: What to do if the total number of months is not divisible to 12 ? How to calculate the step in order to select 12 months from the given list of months proportionally ?
Maybe you should take a step of variable length for each iteration, something like:
step(i) = step(i-1) + NUM_MONTHS/12.0,step(0) = 0.0[note: step(i) is a floating point number and not an integer]And use
floor(step(i))to chose element. [assuming hereNUM_MONTHS > 12]iis the step’s number. You chosea[i] = floor(step(i))as your i’th element for each0 <= i < 12The idea is to create a monotonically increasing function that distributes uniformly. It doesn’t get more uniform then
NUM_MONTHS/12size, with floating points. So, you calculate it with floating points – and then usefloor(step(i))to get the integer out of it.This method ensures 2 things:
NUM_MONTHS/12 > 1]1.Also note that if
NUM_STEPS % 12 == 0this method decays to yourelse ifand behave the same.