# Surprised by randcycle

**URL:** <https://discourse.julialang.org/t/surprised-by-randcycle/43286>\
**Category:** New to Julia\
**Created:** [July 18, 2020, 12:57pm UTC](https://discourse.julialang.org/t/surprised-by-randcycle/43286 "2020-07-18T12:57:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oystein\_Rognes\_Solhe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oystein_rognes_solhe/32/8182_2.png) [@Oystein\_Rognes\_Solhe](https://discourse.julialang.org/u/Oystein_Rognes_Solhe)\
**Post date:** [July 18, 2020, 12:57pm UTC](https://discourse.julialang.org/t/surprised-by-randcycle/43286/1 "2020-07-18T12:57:24Z")

</div>

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
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?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 18, 2020, 2:17pm UTC](https://discourse.julialang.org/t/surprised-by-randcycle/43286/2 "2020-07-18T14:17:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [July 18, 2020, 2:41pm UTC](https://discourse.julialang.org/t/surprised-by-randcycle/43286/3 "2020-07-18T14:41:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Oystein\_Rognes\_Solhe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oystein_rognes_solhe/32/8182_2.png) [@Oystein\_Rognes\_Solhe](https://discourse.julialang.org/u/Oystein_Rognes_Solhe)\
**Post date:** [July 18, 2020, 4:10pm UTC](https://discourse.julialang.org/t/surprised-by-randcycle/43286/4 "2020-07-18T16:10:21Z")

</div>

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:  
[https://math.stackexchange.com/questions/3270801/cyclic-and-circular-permutation-are-they-different-concepts](https://math.stackexchange.com/questions/3270801/cyclic-and-circular-permutation-are-they-different-concepts)

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
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]

```
