|
| 1 | +# generation of some containers filled with random values |
| 2 | + |
| 3 | + |
| 4 | +## dicts |
| 5 | + |
| 6 | +rand!(A::AbstractDict{K,V}, dist::Distribution{<:Pair}=Combine(Pair, K, V)) where {K,V} = |
| 7 | + rand!(GLOBAL_RNG, A, dist) |
| 8 | + |
| 9 | +rand!(rng::AbstractRNG, A::AbstractDict{K,V}, |
| 10 | + dist::Distribution{<:Pair}=Combine(Pair, K, V)) where {K,V} = |
| 11 | + rand!(GLOBAL_RNG, A, Sampler(rng, dist)) |
| 12 | + |
| 13 | +function _rand!(rng::AbstractRNG, A::Union{AbstractDict,AbstractSet}, n::Integer, sp::Sampler) |
| 14 | + empty!(A) |
| 15 | + while length(A) < n |
| 16 | + push!(A, rand(rng, sp)) |
| 17 | + end |
| 18 | + A |
| 19 | +end |
| 20 | + |
| 21 | +rand!(rng::AbstractRNG, A::AbstractDict{K,V}, sp::Sampler) where {K,V} = _rand!(rng, A, length(A), sp) |
| 22 | + |
| 23 | +rand(rng::AbstractRNG, dist::Distribution{<:Pair}, ::Type{T}, n::Integer) where {T<:AbstractDict} = |
| 24 | + _rand!(rng, deduce_type(T, eltype(dist).parameters...)(), n, Sampler(rng, dist)) |
| 25 | + |
| 26 | +rand(u::Distribution{<:Pair}, ::Type{T}, n::Integer) where {T<:AbstractDict} = rand(GLOBAL_RNG, u, T, n) |
| 27 | + |
| 28 | + |
| 29 | +## sets |
| 30 | + |
| 31 | +rand!(A::AbstractSet{T}, X) where {T} = rand!(GLOBAL_RNG, A, X) |
| 32 | +rand!(A::AbstractSet{T}, ::Type{X}=T) where {T,X} = rand!(GLOBAL_RNG, A, X) |
| 33 | + |
| 34 | +rand!(rng::AbstractRNG, A::AbstractSet, X) = rand!(rng, A, Sampler(rng, X)) |
| 35 | +rand!(rng::AbstractRNG, A::AbstractSet{T}, ::Type{X}=T) where {T,X} = rand!(rng, A, Sampler(rng, X)) |
| 36 | + |
| 37 | +_rand0!(rng::AbstractRNG, A::AbstractSet, n::Integer, X) = _rand!(rng, A, n, Sampler(rng, X)) |
| 38 | +_rand0!(rng::AbstractRNG, A::AbstractSet, n::Integer, ::Type{X}) where {X} = _rand!(rng, A, n, Sampler(rng, X)) |
| 39 | +_rand0!(rng::AbstractRNG, A::AbstractSet, n::Integer, sp::Sampler) = _rand!(rng, A, n, sp) |
| 40 | + |
| 41 | +rand!(rng::AbstractRNG, A::AbstractSet, sp::Sampler) = _rand!(rng, A, length(A), sp) |
| 42 | + |
| 43 | + |
| 44 | +rand(r::AbstractRNG, ::Type{T}, n::Integer) where {T<:AbstractSet} = rand(r, Float64, T, n) |
| 45 | +rand( ::Type{T}, n::Integer) where {T<:AbstractSet} = rand(GLOBAL_RNG, T, n) |
| 46 | + |
| 47 | +rand(r::AbstractRNG, X, ::Type{T}, n::Integer) where {T<:AbstractSet} = _rand0!(r, deduce_type(T, eltype(X))(), n, X) |
| 48 | +rand( X, ::Type{T}, n::Integer) where {T<:AbstractSet} = rand(GLOBAL_RNG, X, T, n) |
| 49 | + |
| 50 | +rand(r::AbstractRNG, ::Type{X}, ::Type{T}, n::Integer) where {X,T<:AbstractSet} = _rand0!(r, deduce_type(T, X)(), n, X) |
| 51 | +rand( ::Type{X}, ::Type{T}, n::Integer) where {X,T<:AbstractSet} = rand(GLOBAL_RNG, X, T, n) |
| 52 | + |
| 53 | + |
| 54 | +## sparse vectors & matrices |
| 55 | + |
| 56 | +rand(r::AbstractRNG, p::AbstractFloat, m::Integer) = sprand(r, m, p) |
| 57 | +rand( p::AbstractFloat, m::Integer) = sprand(GLOBAL_RNG, m, p) |
| 58 | +rand(r::AbstractRNG, p::AbstractFloat, m::Integer, n::Integer) = sprand(r, m, n, p) |
| 59 | +rand( p::AbstractFloat, m::Integer, n::Integer) = sprand(GLOBAL_RNG, m, n, p) |
| 60 | + |
| 61 | +rand(r::AbstractRNG, X::Sampler, p::AbstractFloat, m::Integer) = |
| 62 | + sprand(r, m, p, (r, n)->rand(r, X, n)) |
| 63 | + |
| 64 | +rand(r::AbstractRNG, X, p::AbstractFloat, m::Integer) = |
| 65 | + rand(r, Sampler(r, X), p, m) |
| 66 | + |
| 67 | +rand(r::AbstractRNG, ::Type{X}, p::AbstractFloat, m::Integer) where {X} = |
| 68 | + rand(r, Sampler(r, X), p, m) |
| 69 | + |
| 70 | +rand(X, p::AbstractFloat, m::Integer) = rand(GLOBAL_RNG, X, p, m) |
| 71 | + |
| 72 | +rand(r::AbstractRNG, X::Sampler, p::AbstractFloat, m::Integer, n::Integer) = |
| 73 | + sprand(r, m, n, p, (r, n)->rand(r, X, n), eltype(X)) |
| 74 | + |
| 75 | +rand(r::AbstractRNG, X, p::AbstractFloat, m::Integer, n::Integer) = |
| 76 | + rand(r, Sampler(r, X), p, m, n) |
| 77 | + |
| 78 | +rand(r::AbstractRNG, ::Type{X}, p::AbstractFloat, m::Integer, n::Integer) where {X} = |
| 79 | + rand(r, Sampler(r, X), p, m, n) |
| 80 | + |
| 81 | +rand(X, p::AbstractFloat, m::Integer, n::Integer) = rand(GLOBAL_RNG, X, p, m, n) |
| 82 | + |
| 83 | + |
| 84 | +## String |
| 85 | + |
| 86 | +let b = UInt8['0':'9';'A':'Z';'a':'z'] |
| 87 | + global rand |
| 88 | + rand(rng::AbstractRNG, chars, ::Type{String}, n::Integer=8) = String(rand(rng, chars, n)) |
| 89 | + rand( chars, ::Type{String}, n::Integer=8) = rand(GLOBAL_RNG, chars, String, n) |
| 90 | + rand(rng::AbstractRNG, ::Type{String}, n::Integer=8) = rand(rng, b, String, n) |
| 91 | + rand( ::Type{String}, n::Integer=8) = rand(GLOBAL_RNG, b, String, n) |
| 92 | +end |
| 93 | + |
| 94 | + |
| 95 | +## BitArray |
| 96 | + |
| 97 | +const BitArrays = Union{BitArray,BitVector,BitMatrix} |
| 98 | + |
| 99 | +rand(r::AbstractRNG, ::Type{T}, dims::Dims) where {T<:BitArrays} = |
| 100 | + rand!(r, T(uninitialized, dims)) |
| 101 | + |
| 102 | +rand(r::AbstractRNG, ::Type{T}, dims::Integer...) where {T<:BitArrays} = |
| 103 | + rand!(r, T(uninitialized, convert(Dims, dims))) |
| 104 | + |
| 105 | +rand(::Type{T}, dims::Dims) where {T<:BitArrays} = |
| 106 | + rand!(T(uninitialized, dims)) |
| 107 | + |
| 108 | +rand(::Type{T}, dims::Integer...) where {T<:BitArrays} = |
| 109 | + rand!(T(uninitialized, convert(Dims, dims))) |
0 commit comments