algorithmmathdistributionbuckets

Algorithm to calculate weighted distribution ratio


Here's the problem I'm facing. I have some number of items. I then have a varying number of buckets with a weight (between 0 and 1) attached to them. I'm trying to calculate the percentage of the items that should go in each bucket.

For example, let's say I have 20 items and 3 buckets:

The percentage would then be:

The percentage should add to 100% so that all items will be distributed into buckets. In the example above, B1 and B2 should both have twice as many items as B3 since their weight is double that of B3; but, when all 3 buckets are put together, the actual percentage of items B1 gets is 40%.

Is there an algorithm already out there for this or do any of you have an idea of how to solve it?


Solution

  • Sum the weights from all buckets, then divide each bucket's weight by that sum to derive the bucket's percentage of the total.