I'm posting this problem because I'm curious if there's an efficient algorithm that already exists to do it. I can certainly program something up, but if something exists that's already out there and better, I'd much rather use that. Here's the situation...
I have "n" buckets. For this example, let's say I have 3.
Each of these buckets holds an integer value (at no time can they hold a non-integer), so for this example... 10, 5, and 1
I have a total value "x"... so... 16 in this case. It can also only be an integer value.
I decide I want to change "x" to a new number, and have it update the buckets to new values in proportion to what they currently are. So, if I change it to "26", I need to distribute those "10" I added on to my buckets in a 10:5:1 ratio, as evenly as possible... In the end, the sum in my "n" buckets will equal what I changed "x" to, and will be roughly (though not necessarily exactly) in the same ratio of 10:5:1.
Conventional math using rounding can end up with SUM(buckets(n)) <> x due to rounding errors compounding.
As I said, I was wondering if there was am algorithm out there that handles this already.
Essentially, this is to split a forecasted yearly part-count across 12 months, weighted based on last-years monthly sales
Any response would be greatly appreciated... either a code/pseudocode example or just the name of the algorithm is it already exists. As I said, I'm pretty confident I can come up with something myself, but if this is something more elegantly done before, I don't like reinventing the wheel.
Thanks!
I have "n" buckets. For this example, let's say I have 3.
Each of these buckets holds an integer value (at no time can they hold a non-integer), so for this example... 10, 5, and 1
I have a total value "x"... so... 16 in this case. It can also only be an integer value.
I decide I want to change "x" to a new number, and have it update the buckets to new values in proportion to what they currently are. So, if I change it to "26", I need to distribute those "10" I added on to my buckets in a 10:5:1 ratio, as evenly as possible... In the end, the sum in my "n" buckets will equal what I changed "x" to, and will be roughly (though not necessarily exactly) in the same ratio of 10:5:1.
Conventional math using rounding can end up with SUM(buckets(n)) <> x due to rounding errors compounding.
As I said, I was wondering if there was am algorithm out there that handles this already.
Essentially, this is to split a forecasted yearly part-count across 12 months, weighted based on last-years monthly sales
Any response would be greatly appreciated... either a code/pseudocode example or just the name of the algorithm is it already exists. As I said, I'm pretty confident I can come up with something myself, but if this is something more elegantly done before, I don't like reinventing the wheel.
Thanks!