Originally Posted by

**lavoisier**
Hi DrRocket,

I think I'd go for either 1 or 2.

In principle, one could virtually build all possible distributions of the N objects in the M compartments, compute the 'variance' of each, and select the one(s) with the lowest variance (in fact, writing this made me realise that there is not necessarily a single solution... a bit like a bad Sudoku).

However, I bet even for low N's this would be very time-consuming and probably computationally inefficient. I can't even think of a formula to *count* all possible distributions.

In fact, a few years ago I tried to write an algorithm to solve this problem, based on a physical analogy.

I imagined that the objects were like particles of liquid in M communicating vessels, and they could move from one to another in such a way that the level would get as even as possible.

The algorithm analysed all possible pairs of compartments, and for each object i in compartment 1, it checked whether moving i to compartment 2 would result in a reduction of the variance. Then same again, but checking the effect of swapping object i in compartment 1 with object j in compartment 2. And so on until no further reduction was possible, through the usual sequence of loops you use in algorithms.

I was not happy with the result; I wasn't sure this was the most efficient way to do it, and I wasn't sure the output was indeed the best distribution. Did I need to allow other types of operations between compartments? Etc.

That's why I was asking for input.