Fixed an algorithm for random rows and columns switching