Moved randomization from the loops.