Refactored randomization algorithm.
Merge origin/master