I want to make an algorithm that distributes total number of entities into different categories using some ideal percentage. Lets say for example, that first category should contain 50.3% of all entities, second - 34.3%, third - 15.4%.
In some ideal conditions everything is good. I'm easily calculating desired value of entity for every category and then using some algorithm similar to this to maintain the right sum. Small example:
All categories are empty
We are adding 100
To the first 50
To the second = 34
To the third = 16 (fixed by current algorithm, was 15)
BUT, at the some point category can already contain some entities which are not distributed according our ideal percentage! And I do not know what algorithm I should use in this case. This algorithm should distribute new entities using these rules:
- After adding we should be as close as possible to ideal percentage. Deviations between ideal and actual percentage should be minimal and close to each other.
- Algorithm CANT delete existing entities from any category. It should only distribute the new ones.
Example:
At start:
First is empty
Second contains 10
Third contains 40
We are adding 50
To the first 38
To the second 12
To the third 0 (too much entities here before adding)
Result:
First = 38; deviation = 12.3%
Second = 22; deviation = 12.3%
Third = 40; deviation = 24.6%
Do not ask me how I got 38 and 12, I've just tried different combinations until it seems right. Any ideas about algorithm?