# Can rand generate random vectors with custom type entries?

**URL:** https://discourse.julialang.org/t/can-rand-generate-random-vectors-with-custom-type-entries/107028
**Category:** General Usage
**Tags:** random
**Created:** [December 2, 2023, 1:18am UTC](https://discourse.julialang.org/t/can-rand-generate-random-vectors-with-custom-type-entries/107028 "2023-12-02T01:18:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [December 2, 2023, 1:18am UTC](https://discourse.julialang.org/t/can-rand-generate-random-vectors-with-custom-type-entries/107028/1 "2023-12-02T01:18:02Z")

</div>

I’m trying to understand how to implement `rand` for custom types. After looking at the documentation, I’ve come up with the following:

```julia
using Random

struct A{T}
    x::T
end

Random.rand(rng::AbstractRNG, ::Type{A{T}}) where T = A(rand(rng, T))

```

This generates single elements as expected:

```julia
julia> rand(A{Bool})
A{Bool}(false)

```

I thought that generating random vectors would work automatically, but it doesn’t:

```julia
julia> rand(A{Bool}, 3)
ERROR: MethodError: no method matching Random.Sampler(::Type{TaskLocalRNG}, ::Random.SamplerType{A{Bool}}, ::Val{1})

Closest candidates are:
  Random.Sampler(::Type{<:AbstractRNG}, ::Random.Sampler, ::Union{Val{1}, Val{Inf}})
   @ Random /usr/local/julia-1.10.0-rc1/share/julia/stdlib/v1.10/Random/src/Random.jl:147
  Random.Sampler(::Type{<:AbstractRNG}, ::Any, ::Union{Val{1}, Val{Inf}})
   @ Random /usr/local/julia-1.10.0-rc1/share/julia/stdlib/v1.10/Random/src/Random.jl:183
  Random.Sampler(::Type{<:AbstractRNG}, ::BitSet, ::Union{Val{1}, Val{Inf}})
   @ Random /usr/local/julia-1.10.0-rc1/share/julia/stdlib/v1.10/Random/src/generation.jl:450
  ...

Stacktrace:
 [1] Random.Sampler(T::Type{TaskLocalRNG}, sp::Random.SamplerType{A{Bool}}, r::Val{1})
   @ Random /usr/local/julia-1.10.0-rc1/share/julia/stdlib/v1.10/Random/src/Random.jl:147
 [2] Random.Sampler(rng::TaskLocalRNG, x::Random.SamplerType{A{Bool}}, r::Val{1})
   @ Random /usr/local/julia-1.10.0-rc1/share/julia/stdlib/v1.10/Random/src/Random.jl:139
 [3] rand(rng::TaskLocalRNG, X::Random.SamplerType{A{Bool}})
   @ Random /usr/local/julia-1.10.0-rc1/share/julia/stdlib/v1.10/Random/src/Random.jl:255
 [4] rand!
   @ Random /usr/local/julia-1.10.0-rc1/share/julia/stdlib/v1.10/Random/src/Random.jl:273 [inlined]
 [5] rand!
   @ Random /usr/local/julia-1.10.0-rc1/share/julia/stdlib/v1.10/Random/src/Random.jl:269 [inlined]
 [6] rand
   @ Random /usr/local/julia-1.10.0-rc1/share/julia/stdlib/v1.10/Random/src/Random.jl:290 [inlined]
 [7] rand
   @ Random /usr/local/julia-1.10.0-rc1/share/julia/stdlib/v1.10/Random/src/Random.jl:291 [inlined]
 [8] rand(::Type{A{Bool}}, ::Int64)
   @ Random /usr/local/julia-1.10.0-rc1/share/julia/stdlib/v1.10/Random/src/Random.jl:294
 [9] top-level scope
   @ REPL[5]:1

```

I find this particularly surprising because the first closest candidate displayed above should match in my opinion because

```julia
julia> TaskLocalRNG <: AbstractRNG, Random.SamplerType{A{Bool}} <: Random.Sampler
(true, true)

```

(The second should also match, but it’s less specific.) Am I missing anything?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 2, 2023, 1:24am UTC](https://discourse.julialang.org/t/can-rand-generate-random-vectors-with-custom-type-entries/107028/2 "2023-12-02T01:24:29Z")

</div>

(Note that you can always just do `[rand(T) for _=1:n]`.)

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [December 2, 2023, 1:26am UTC](https://discourse.julialang.org/t/can-rand-generate-random-vectors-with-custom-type-entries/107028/3 "2023-12-02T01:26:27Z")

</div>

Sure. But should `rand(A{Bool}, 3)` work? (So my question is rather about the API of `Random`. I’ve clarified the topic title.)

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [December 2, 2023, 2:18am UTC](https://discourse.julialang.org/t/can-rand-generate-random-vectors-with-custom-type-entries/107028/4 "2023-12-02T02:18:17Z")

</div>

[https://docs.julialang.org/en/v1/stdlib/Random/#Hooking-into-the-Random-API](https://docs.julialang.org/en/v1/stdlib/Random/#Hooking-into-the-Random-API)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 2, 2023, 3:47am UTC](https://discourse.julialang.org/t/can-rand-generate-random-vectors-with-custom-type-entries/107028/5 "2023-12-02T03:47:03Z")

</div>

> [@matthias314](#):
>
> `ERROR: MethodError: no method matching Random.Sampler(::Type{TaskLocalRNG}, ::Random.SamplerType{A{Bool}}, ::Val{1})`

It does seem like there should be a default `Sampler` for `SamplerType{T}` that simply calls `rand(rng, T)` …

cc @rfourquet

---

<div class="post-metadata">

### Author: ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)
#### Post date: [December 2, 2023, 4:41am UTC](https://discourse.julialang.org/t/can-rand-generate-random-vectors-with-custom-type-entries/107028/6 "2023-12-02T04:41:09Z")

</div>

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.

```julia
using Random

struct A{T}
    x::T
end

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

```

```julia-repl
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)

```
