Surprised by randcycle

Hi, first time poster here!

I am fooling around with cyclic permutations today in Julia 1.4.2 and was surprised to see that the randcycle function in Random do not include the identity permutation. As an example:

julia> Set([randcycle(3) for _ in 1:1000])
Set{Array{Int64,1}} with 2 elements:
  [2, 3, 1]
  [3, 1, 2]

I was expecting 3-element Array{Int64,1}: 1 2 3 to also be in the returned set. Am I missing something elementary here?

2 Likes

AFAIK most texts define cyclic permutations excluding just the trivial cycles.

1 Like

The permuation [1, 2, 3] is not a cycle of length 3, that is, it is not a 3-cycle. To elaborate, the orbit does not have length 3.

PS: Maybe you are looking for the randperm function?

2 Likes

Thanks for the answers! The reason I was confused (in addition to my obvious lack of knowledge in group and graph theory) was this short discussion on stack exchange:

Considering people around a table, I am not looking for the (n-1)! possible ways of ordering n people, but rather the much simpler n shifts where there is an initial fixed ordering of the people.

So randperm will not do i am afraid. But stumbling over the circshift function, I seem to have resolved my problems:

julia> rand_circshift(array::AbstractArray) = circshift(array, rand(1:length(array)))
rand_circshift (generic function with 1 method)

julia> rand_circshift(n::Int) = rand_circshift(1:n)
rand_circshift (generic function with 2 methods)

julia> Set([rand_circshift(3) for _ in 1:10000])
Set{Array{Int64,1}} with 3 elements:
  [3, 1, 2]
  [2, 3, 1]
  [1, 2, 3]
1 Like