Help me find a good algorithm?
I have a bag full of n balls. (Let's say 28 goals for an example.)
The balls in this bag each have 1 color. There are <= 4 different colors of balls in the bag. (Let them say that red, green, blue and violet are opportunities.)
I have three buckets, each of which has the number of balls they need. These numbers are n. (For example, say, bucket A should end with 7 balls, bucket B should end with 11 balls, bucket C should end with 10 balls.)
Buckets may or may not have color restrictions — colors that they won’t accept. (Bucket A does not accept purple balls or green balls. Bucket B does not accept red balls. C bucket does not accept purple balls or blue balls.)
I need to distribute balls efficiently and randomly (equal probability of all possibilities).
I can’t just accidentally put balls in buckets that have a place to accept them, because this can lead me to a situation where the only bucket in which there is space does not accept the only color that remains in the bag.
It is given that there is always the opportunity to distribute balls at least 1 time. (I will not have a bag of red balls only, and some bucket with a number> 0 will not accept red balls.)
, . ( , C 1, 2, , , , C 2, 1.)
, :
, , . - .
, , .
, .
Choose a ball from the bag at random. (Call it "this ball".)
If this ball fits and is allowed in a number of buckets > 0:
Choose one of those buckets at random and put this ball in that bucket.
else (this ball is not allowed in any bucket that it fits in):
Make a list of colors that can go in buckets that are not full.
Make a list of balls of those colors that are in full buckets that this ball is allowed in.
If that 2nd list is length 0 (There are no balls of colors from the 1st list in the bucket that allows the color of this ball):
ASSERT: (Please show me an example situation where this might not be the case.)
There is a 3rd bucket that is not involved in the previously used buckets in this algorithm.
(One bucket is full and is the only one that allows this ball.
A second bucket is the only one not full and doesn't allow this ball or any ball in the first bucket.
The 3rd bucket is full must allow some color that is in the first bucket and must have some ball that is allowed in the second bucket.)
Choose, at random, a ball from the 3rd bucket balls of colors that fit in the 2nd bucket, and move that ball to the 2nd bucket.
Choose, at random, a ball from the 1st bucket balls of colors that fit in the 3rd bucket, and move that ball to the 3rd bucket.
Put "this ball" (finally) in the 1st bucket.
else:
Choose a ball randomly from that list, and move it to a random bucket that is not full.
Put "this ball" in a bucket that allows it.
Next ball.