Reference

Basic Functions

Shuffle.shuffleFunction
shuffle([rng=GLOBAL_RNG,], c, s::AbstractShuffle=RandomShuffle())

shuffle collection c using shuffling algorithm s. To shuffle c in-place, see shuffle!. A random-number generator rng may be supplied for random shuffling algorithms.

Examples

julia> mt = MersenneTwister(1234);

julia> shuffle(mt, [1, 2, 3, 4, 5, 6, 7, 8], GilbertShannonReeds())
8-element Array{Int64,1}:
 6
 7
 1
 2
 8
 3
 4
 5

julia> shuffle([6, 5, 4, 3, 2, 1], Faro{:in}())
6-element Array{Int64,1}:
 3
 6
 2
 5
 1
 4
source
Shuffle.shuffle!Function
shuffle!([rng=GLOBAL_RNG,], c, s::AbstractShuffle=RandomShuffle())

shuffle collection c in-place using shuffling algorithm s. A random-number generator rng may be supplied for random shuffling algorithms.

Examples

julia> mt = MersenneTwister(1234);

julia> a = collect(1:6);

julia> shuffle!(mt, a); a
6-element Array{Int64,1}:
 2
 1
 3
 6
 4
 5

julia> shuffle!(a, Faro{:out}()); a
6-element Array{Int64,1}:
 2
 6
 1
 4
 3
 5
source
Shuffle.nshuffleFunction
nshuffle([rng=GLOBAL_RNG,], c, n::Integer, s::AbstractShuffle=RandomShuffle())

shuffle collection c n times using shuffling algorithm s. A random-number generator rng may be supplied for random shuffling algorithms. To shuffle c in-place see nshuffle!

Examples

julia> nshuffle(collect(1:8), 3, Faro{:in}())
8-element Array{Int64,1}:
 8
 7
 6
 5
 4
 3
 2
 1
source
Shuffle.nshuffle!Function
nshuffle!([rng=GLOBAL_RNG,], c, n::Integer, s::AbstractShuffle=RandomShuffle())

shuffle collection c in-place n times using shuffling algorithm s. A random-number generator rng may be supplied for random shuffling algorithms.

Examples

julia> mt = MersenneTwister(1234);

julia> a = collect(1:7);

julia> nshuffle!(mt, a, 3, GilbertShannonReeds()); a
7-element Array{Int64,1}:
 5
 6
 1
 7
 2
 3
 4
source

Shuffling Algorithms

Shuffle.AbstractShuffleType
AbstractShuffle

Supertype for AbstractDeterministicShuffle and AbstractRandomShuffle.

Implementation

New shuffling algorithm types should be sub types of either AbstractDeterministicShuffle or AbstractRandomShuffle. If an algorithm can be defined in-place, only shuffle! needs to be extended. shuffle, nshuffle and nshuffle! will make a copy / repeatedly call shuffle! as needed. If the algorithm can not be defined in-place, define shuffle and nshuffle!. nshuffle will make a copy and call nshuffle!.

source

Deterministic Shuffles

Shuffle.AbstractDeterministicShuffleType
AbstractDeterministicShuffle <: AbstractShuffle

Supertype for fully deterministic shuffle algorithms such as Faro and Cut.

Implementation

New deterministic shuffling algorithms should implement either shuffle or shuffle!, as appropriate. The new shuffle method should take a collection to be shuffled as the first argument and an instance of the new shuffling algorithm type as the second argument.

For example:

struct MyShuffle <: AbstractDeterministicShuffle
    parameter
end

function shuffle(c::AbstractArray, s::MyShuffle, prealloc_out=similar(c))
    # Do the shuffling

    return prealloc_out
end

Pre-allocating the output makes it easy to also write an efficient nshuffle! function for algorithms that cannot be written in-place.

See also: AbstractShuffle

source
Shuffle.FaroType
Faro{S}
Weave{S}

Type with no fields representing the Faro (weave) card shuffle where S can be :in for an in-shuffle or :out for an out-shuffle.

See singleton instances infaro, outfaro, inweave, outweave.

Examples

julia> shuffle([1, 2, 3, 4, 5, 6, 7, 8], Faro{:out}())
8-element Array{Int64,1}:
 1
 5
 2
 6
 3
 7
 4
 8

julia> nshuffle(collect(1:52), 26, inweave) == collect(52:-1:1)
true
source
Shuffle.CutType
Cut(n::Integer)

represents a cut at n, meaning elements up to and including n are moved to the bottom or end of a collection, while the rest shift up to the beginning.

No in-place shuffle! or nshuffle! exist for this shuffling type.

Examples

julia> shuffle([1, 2, 3, 4, 5, 6, 7], Cut(3))
7-element Array{Int64,1}:
 4
 5
 6
 7
 1
 2
 3
source

Random Shuffles

Shuffle.AbstractRandomShuffleType
AbstractRandomShuffle <: AbstractShuffle

Supertype for partly or fully random shuffle algorithms such as RandomShuffle and GilbertShannonReeds.

Implementation

New random shuffling algorithms should implement either shuffle or shuffle!, as appropriate. The new shuffle method should take a Random.AbstractRNG as the first argument, a collection to be shuffled as the second argument and an instance of the new shuffling algorithm type as the third argument.

For example:

struct MyShuffle <: AbstractRandomShuffle end

function shuffle!(r::AbstractRNG, c, s::MyShuffle)
    # Do the shuffling, passing r to any random number generating functions

    return c
end

See also: AbstractShuffle

source
Shuffle.RandomShuffleType
RandomShuffle

type with no fields representing a completely random shuffle with singleton instance randshuffle.

This algorithm is set as the default. See DEFAULTS.

Examples

julia> mt = MersenneTwister(1234);

julia> shuffle(mt, 1:7, randshuffle)
7-element Array{Int64,1}:
 1
 2
 3
 7
 6
 4
 5
source

Defaults

Shuffle.DEFAULTSConstant
DEFAULTS

binding to a mutable struct containing the default shuffling algorithm. RandomShuffle is set initially.

Examples

julia> Shuffle.DEFAULTS.shuffle
RandomShuffle()

julia> mt1 = MersenneTwister(1234); mt2 = MersenneTwister(1234);

julia> shuffle(mt1, collect(1:100)) == shuffle(mt2, collect(1:100), RandomShuffle())
true

julia> Shuffle.DEFAULTS.shuffle = infaro; Shuffle.DEFAULTS.shuffle
Faro{:in}()

julia> shuffle([1, 2, 3, 4, 5, 6, 7, 8])
8-element Array{Int64,1}:
 5
 1
 6
 2
 7
 3
 8
 4

julia> Shuffle.DEFAULTS.shuffle = RandomShuffle();
source