Fix bug 2932, and added randomization.