# How to sample multiple self defined random variable

**URL:** <https://discourse.julialang.org/t/how-to-sample-multiple-self-defined-random-variable/59847>\
**Category:** General Usage\
**Tags:** distributions\
**Created:** [April 23, 2021, 8:09am UTC](https://discourse.julialang.org/t/how-to-sample-multiple-self-defined-random-variable/59847 "2021-04-23T08:09:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wei\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wei_yang/32/23951_2.png) [@Wei\_Yang](https://discourse.julialang.org/u/Wei_Yang)\
**Post date:** [April 23, 2021, 8:09am UTC](https://discourse.julialang.org/t/how-to-sample-multiple-self-defined-random-variable/59847/1 "2021-04-23T08:09:52Z")

</div>

I have defined the following Distribution,

```julia
using Distributions

struct ComplexNormal <:ContinuousUnivariateDistribution
    μ::ComplexF64
    σ::Float64
    function ComplexNormal(μ=0,σ=1) new(μ,σ) end
end

function Base.rand(d::ComplexNormal)
    randn(ComplexF64)*d.σ+d.μ
end

```

I’m trying to figure out how to make `rand(ComplexNormal(),2)` and `rand(ComplexNormal(),2,2)` work.

According to the [documentation](https://juliastats.org/Distributions.jl/v0.14/extends.html), _ **The package already implements a vectorized version of `rand!` and `rand` that repeatedly calls the he scalar version to generate multiple samples** _. Thus I probably do not need to define `Base.rand(::ComplexNormal, n::Int)`, but after googling for 1 hour, I couldn’t figure out how to do it.

Any help will be greatly appreciated, thank you!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 23, 2021, 9:08am UTC](https://discourse.julialang.org/t/how-to-sample-multiple-self-defined-random-variable/59847/2 "2021-04-23T09:08:12Z")

</div>

[https://docs.julialang.org/en/v1/stdlib/Random/#Generating-random-values-of-custom-types](https://docs.julialang.org/en/v1/stdlib/Random/#Generating-random-values-of-custom-types)

---

<div class="post-metadata">

**Author:** ![Wei\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wei_yang/32/23951_2.png) [@Wei\_Yang](https://discourse.julialang.org/u/Wei_Yang)\
**Post date:** [April 24, 2021, 9:11pm UTC](https://discourse.julialang.org/t/how-to-sample-multiple-self-defined-random-variable/59847/3 "2021-04-24T21:11:36Z")

</div>

Thank you, certainly gets me closer to what I wanted.

```julia
using Distributions, Random

struct ComplexNormal <:ContinuousUnivariateDistribution
    μ::ComplexF64
    σ::Float64
    function ComplexNormal(μ=0,σ=1) new(μ,σ) end
end

Base.rand(rng::AbstractRNG, d::ComplexNormal) =randn(ComplexF64)*d.σ+d.μ;

```

`rand(ComplexNormal(1+2im,1))` generates a single element works.  
`rand(ComplexNormal(1+2im,1),2)` don’t quiet, as it gives  
`InexactError: Float64(0.37414329354088083 + 1.039498256924925im)`

This is probably outside of what I asked in this post, I’m going to start a new one.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 25, 2021, 7:15am UTC](https://discourse.julialang.org/t/how-to-sample-multiple-self-defined-random-variable/59847/4 "2021-04-25T07:15:06Z")

</div>

I am wondering if you actually read the docs though. You are probably looking for `SamplerTrivial`, as in

[https://docs.julialang.org/en/v1/stdlib/Random/#A-simple-sampler-without-pre-computed-data](https://docs.julialang.org/en/v1/stdlib/Random/#A-simple-sampler-without-pre-computed-data)

---

<div class="post-metadata">

**Author:** ![Wei\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wei_yang/32/23951_2.png) [@Wei\_Yang](https://discourse.julialang.org/u/Wei_Yang)\
**Post date:** [April 25, 2021, 7:50am UTC](https://discourse.julialang.org/t/how-to-sample-multiple-self-defined-random-variable/59847/5 "2021-04-25T07:50:47Z")

</div>

Yes, I do noticed that I can achieve what I want with the following

```julia
using Random
struct CNormal
    μ::ComplexF64
    σ::Float64 
end

Random.rand(rng::AbstractRNG, d::Random.SamplerTrivial{CNormal}) = randn(rng,ComplexF64)*d[].σ+d[].μ

rand(CNormal(0,1),2)

```

However, I have not yet figure out how to make this work if I want `CNormal <:ContinuousUnivariateDistribution`

* * *

The following is _ **a** _ solution, but there probably is a way to make the above work

```julia
using Distributions, Random

struct ComplexNormal <:ContinuousUnivariateDistribution
    μ::ComplexF64
    σ::Float64
    function ComplexNormal(μ=0,σ=1) new(μ,σ) end
end

Base.rand(rng::AbstractRNG, d::ComplexNormal) = randn(rng,ComplexF64)*d.σ+d.μ;
Base.eltype(::Type{ComplexNormal}) = ComplexF64

```
