Excellent Rick.
Your enhanced more robust code encounters an error when the balls' freq count is highly skewed and has large values like for example {5000 10 200}.
Then the second color in my example gets assigned all 0 for each trial.
To fix it, I've reset the idx_ vectors just after entering the do loop and now it works.

do i = 1 to ncol(K)-1;
idx=.;
idx0=.;
idx1=.;
...