问题:如何从Julia的高斯分布中生成一个在[0,1]区间内的随机数?
我收集randn是生成正态分布的随机数的方法,但是文档中有关如何指定范围的描述非常不透明。
答案 0 :(得分:6)
使用Distributions软件包。如果您还没有它:
using Pkg ; Pkg.add("Distributions")
然后:
using Distributions
mu = 0 #The mean of the truncated Normal
sigma = 1 #The standard deviation of the truncated Normal
lb = 0 #The truncation lower bound
ub = 1 #The truncation upper bound
d = Truncated(Normal(mu, sigma), lb, ub) #Construct the distribution type
x = rand(d, 100) #Simulate 100 obs from the truncated Normal
或全部一行:
x = rand(Truncated(Normal(0, 1), 0, 1), 100)