[R] Curiously short cycles in iterated permutations with the same seed

Boris Steipe boris.steipe at utoronto.ca
Fri Dec 8 05:39:11 CET 2017


I have noticed that when I iterate permutations of short vectors with the same seed, the cycle lengths are much shorter than I would expect by chance. For example:

X <- 1:10
Xorig <- X
start <- 112358
N <- 10

for (i in 1:N) {
  seed <- start + i
  for (j in 1:1000) { # Maximum cycle length to consider
    set.seed(seed)    # Re-seed RNG to same initial state
    X <- sample(X)    # Permute X and iterate
    if (all(X == Xorig)) {
      cat(sprintf("Seed:\t%d\tCycle: %d\n", seed, j))
      break()
    }
  }
}

Seed:	112359	Cycle: 14
Seed:	112360	Cycle: 14
Seed:	112361	Cycle: 8
Seed:	112362	Cycle: 14
Seed:	112363	Cycle: 8
Seed:	112364	Cycle: 10
Seed:	112365	Cycle: 10
Seed:	112366	Cycle: 10
Seed:	112367	Cycle: 9
Seed:	112368	Cycle: 12

I understand that I am performing the same permutation operation over and over again - but I don't see why that would lead to such a short cycle (in fact the cycle for the first 100,000 seeds is never longer than 30). Does this have a straightforward explanation?


Thanks!
Boris



More information about the R-help mailing list