Skip to content

Commit 5a33c70

Browse files
authored
Clarify definition of cyclic permutation in randcycle docstring (#50487)
1 parent 3995278 commit 5a33c70

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

stdlib/Random/src/misc.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,12 @@ Construct a random cyclic permutation of length `n`. The optional `rng`
345345
argument specifies a random number generator, see [Random Numbers](@ref).
346346
The element type of the result is the same as the type of `n`.
347347
348+
Here, a "cyclic permutation" means that all of the elements lie within
349+
a single cycle. If `n > 0`, there are ``(n-1)!`` possible cyclic permutations,
350+
which are sampled uniformly. If `n == 0`, `randcycle` returns an empty vector.
351+
352+
[`randcycle!`](@ref) is an in-place variant of this function.
353+
348354
!!! compat "Julia 1.1"
349355
In Julia 1.1 and above, `randcycle` returns a vector `v` with
350356
`eltype(v) == typeof(n)` while in Julia 1.0 `eltype(v) == Int`.
@@ -367,10 +373,16 @@ randcycle(n::Integer) = randcycle(default_rng(), n)
367373
"""
368374
randcycle!([rng=default_rng(),] A::Array{<:Integer})
369375
370-
Construct in `A` a random cyclic permutation of length `length(A)`.
376+
Construct in `A` a random cyclic permutation of length `n = length(A)`.
371377
The optional `rng` argument specifies a random number generator, see
372378
[Random Numbers](@ref).
373379
380+
Here, a "cyclic permutation" means that all of the elements lie within a single cycle.
381+
If `A` is nonempty (`n > 0`), there are ``(n-1)!`` possible cyclic permutations,
382+
which are sampled uniformly. If `A` is empty, `randcycle!` leaves it unchanged.
383+
384+
[`randcycle`](@ref) is a variant of this function that allocates a new vector.
385+
374386
# Examples
375387
```jldoctest
376388
julia> randcycle!(MersenneTwister(1234), Vector{Int}(undef, 6))
@@ -390,6 +402,7 @@ function randcycle!(r::AbstractRNG, a::Array{<:Integer})
390402
n == 0 && return a
391403
a[1] = 1
392404
mask = 3
405+
# Sattolo's algorithm:
393406
@inbounds for i = 2:n
394407
j = 1 + rand(r, ltm52(i-1, mask))
395408
a[i] = a[j]

0 commit comments

Comments
 (0)