5

我已经挖了谷歌,但没有找到一个例子。我确信 Julia 具有强大的功能(在基数中?)以给定的概率生成随机二项式(伯努利?)“成功”。我找不到它或弄清楚如何在 Julia 中做相当于:

> rbinom(20,1,0.3)
 [1] 1 1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 1 0 0

谢谢。Ĵ

4

1 回答 1

8

您可以为此使用 Distributions 和rand函数。任何分布都可以传递给rand. 要复制您想要的内容:

julia> using Distributions

julia> p = Binomial(1, 0.3)   # first arg is number of trials, second is probability of success
Binomial{Float64}(n=1, p=0.3)

julia> rand(p, 20)
20-element Array{Int64,1}:
 0
 1
 1
 0
 1
 0
 0
 1
 0
 1
 1
 1
 0
 0
 1
 0
 1
 0
 0
 1
于 2021-01-27T17:30:20.433 回答