Hi, I am trying to scramble items in a matrix so that no item repeats on consecutive rows. This generates the matrix: LevelsOfA = 2 LevelsOfB = 2 LevelsOfC = 3 Items = 10 FactorA = rep(1:LevelsOfA, each=(LevelsOfB*LevelsOfC*Items)) FactorB = rep(rep(1:LevelsOfB, each=(LevelsOfC*Items)),LevelsOfA) FactorC = rep(rep(1:LevelsOfC, each=Items), LevelsOfA*LevelsOfB) TargItems = rep(1:Items, LevelsOfA*LevelsOfB*LevelsOfC) IndexArray = data.frame(FactorA=FactorA, FactorB=FactorB, FactorC=FactorC, TargItems=TargItems) scram = sample(nrow(IndexArray), replace = FALSE) IndexArray = IndexArray[scram,] That's a start, but now I want to continue scrambling IndexArray so that no item in the first 3 columns repeats across more than 3 consecutive rows and that no item repeats more than once in the last column. Is there a reasonably straightforward solution to this? I have tried various loops nested within loops in combination with some pretty ugly if/then statements, but without much success. Thanks for any suggestions. Matthew [[alternative HTML version deleted]]