Julia,在 R 中复制“rbinom()”的函数

问题描述

我四处挖掘并用谷歌搜索,但没有找到一个例子。我确信 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

谢谢。 J

解决方法

您可以为此使用分布和 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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...