Current state of patch is more proof of concept than something ready. I'm going to change it in following ways:
1) We don't know how expensive user penalty function is. So, I'm going to change randomization algorithm so that it doesn't increase number of penalty calls in average.
2) Since, randomization could produce additional IO, there are probably no optimal solution for all the cases. We could introduce user-visible option which enables or disables randomization. However, default value of this option is another question.
Also, I think you should use random() rather than rand().
Thanks, will fix.
New version of patch is attached. Parameter "randomization" was introduced. It controls whether to randomize choose. Choose algorithm was rewritten.