Sampling from a normal distribution

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

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

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 documentation unfortunately did not include this (that I could find).


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

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 ?

The bounds are inclusive, so you could get 0.0.

Which reminds me, I should dust off

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

1 Like