@@ -345,6 +345,12 @@ Construct a random cyclic permutation of length `n`. The optional `rng`
345
345
argument specifies a random number generator, see [Random Numbers](@ref).
346
346
The element type of the result is the same as the type of `n`.
347
347
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
+
348
354
!!! compat "Julia 1.1"
349
355
In Julia 1.1 and above, `randcycle` returns a vector `v` with
350
356
`eltype(v) == typeof(n)` while in Julia 1.0 `eltype(v) == Int`.
@@ -367,10 +373,16 @@ randcycle(n::Integer) = randcycle(default_rng(), n)
367
373
"""
368
374
randcycle!([rng=default_rng(),] A::Array{<:Integer})
369
375
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)`.
371
377
The optional `rng` argument specifies a random number generator, see
372
378
[Random Numbers](@ref).
373
379
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
+
374
386
# Examples
375
387
```jldoctest
376
388
julia> randcycle!(MersenneTwister(1234), Vector{Int}(undef, 6))
@@ -390,6 +402,7 @@ function randcycle!(r::AbstractRNG, a::Array{<:Integer})
390
402
n == 0 && return a
391
403
a[1 ] = 1
392
404
mask = 3
405
+ # Sattolo's algorithm:
393
406
@inbounds for i = 2 : n
394
407
j = 1 + rand (r, ltm52 (i- 1 , mask))
395
408
a[i] = a[j]
0 commit comments