Can rand generate random vectors with custom type entries?

Just to make sure the solution is spelled out here: the one change you need is to replace Type with Random.SamplerType in your method definition.

using Random

struct A{T}
    x::T
end

Random.rand(rng::AbstractRNG, ::Random.SamplerType{A{T}}) where {T} = A(rand(rng, T))
julia> rand(A{Bool})
A{Bool}(false)

julia> rand(A{Bool}, 3)
3-element Vector{A{Bool}}:
 A{Bool}(true)
 A{Bool}(false)
 A{Bool}(false)
5 Likes