# Sampling from a normal distribution

**URL:** <https://discourse.julialang.org/t/sampling-from-a-normal-distribution/36848>\
**Category:** Statistics\
**Created:** [April 1, 2020, 2:12pm UTC](https://discourse.julialang.org/t/sampling-from-a-normal-distribution/36848 "2020-04-01T14:12:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)\
**Post date:** [April 1, 2020, 2:12pm UTC](https://discourse.julialang.org/t/sampling-from-a-normal-distribution/36848/1 "2020-04-01T14:12:04Z")

</div>

I am trying to draw 1000 random values from a normal distribution with a specific mean and standard deviation. Additionally, the values cannot be negative, which is an added constraint. To do this I have been trying to use the Distributions package but am running into errors.  
Here my my attempt

```julia
using Random, Distributions
Random.seed!(123) # Setting the seed
d = Normal(μ=0.16, σ=0.05)
n=rand(d,1000)

```

but this runs into a method error before I can use this distribution to draw data. How would I sample from a specific normal distribution? The default examples on the [Distributions](https://juliastats.org/Distributions.jl/stable/starting/#Starting-With-a-Normal-Distribution-1) documentation unfortunately did not include this (that I could find).

Thanks

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [April 1, 2020, 2:16pm UTC](https://discourse.julialang.org/t/sampling-from-a-normal-distribution/36848/2 "2020-04-01T14:16:23Z")

</div>

> [@samantp](#):
>
> using Random, Distributions Random.seed!(123) # Setting the seed d = Normal(μ=0.16, σ=0.05)

```julia
julia> using Distributions, Random

julia> d = Normal(0.16, 0.05)
Normal{Float64}(μ=0.16, σ=0.05)

julia> td = truncated(d, 0.0, Inf)
Truncated(Normal{Float64}(μ=0.16, σ=0.05), range=(0.0, Inf))

julia> rand(td, 20)'
1×20 LinearAlgebra.Adjoint{Float64,Array{Float64,1}}:
 0.111023 0.17302 0.136576 0.115955 0.173886 0.236196 0.0711136 0.013347 0.199113 0.275679 0.116447 0.177286 0.152207 0.276651 0.110082 0.163348 0.0727901 0.115974 0.147312 0.200701

```

---

<div class="post-metadata">

**Author:** ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)\
**Post date:** [April 1, 2020, 2:20pm UTC](https://discourse.julialang.org/t/sampling-from-a-normal-distribution/36848/4 "2020-04-01T14:20:37Z")

</div>

Thanks so much! Exactly what I was looking for. Are those lower bounds exclusive or inclusive? i.e. is it theoreticallly still possible for rand to draw 0.0 ?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [April 1, 2020, 4:06pm UTC](https://discourse.julialang.org/t/sampling-from-a-normal-distribution/36848/5 "2020-04-01T16:06:43Z")

</div>

The bounds are inclusive, so you could get `0.0`.

---

<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 1, 2020, 4:28pm UTC](https://discourse.julialang.org/t/sampling-from-a-normal-distribution/36848/6 "2020-04-01T16:28:11Z")

</div>

Which reminds me, I should dust off

[https://github.com/JuliaLang/julia/pull/33251](https://github.com/JuliaLang/julia/pull/33251)

(just adding the new open-open and not changing the default)
