[EuroPython] pyhton question- distribution

Thomas Hunger tehunger at gmail.com
Thu Apr 22 18:02:16 CEST 2010

Hey Bill,

If we have 100 apples, for example, and we need to distrubte the 100 apples
> randomly in 10 boxes, how can we do this in python?

I assume the only invariant is sum(box_apples_i) == 100 for 10 boxes?

Maybe the following would do?

from random import sample
partitions = [0] + sorted(sample(range(100), 9)) + [100]
print [b-a for a, b in zip(partitions, partitions[1:])]


PS I'm not sure if this is the best mailing list for such a problem? Maybe
someone who knows could chip in.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/europython/attachments/20100422/3baa7ab3/attachment.html>

More information about the EuroPython mailing list