如何在Netlogo中创建随机的二进制/布尔变量 [英] How to create random binary/boolean variable in Netlogo
问题描述
我想给每只乌龟分配一个随机的布尔变量,但是我没有看到一个可以模拟伯努利分布绘制的函数.
I'd like to assign a random boolean variable to each turtle, but I'm not seeing a function that would simulate a draw from a Bernoulli distribution.
这很接近,但是很尴尬:
This gets close, but it's awkward:
ifelse random-in-range 0 1 < .5 [set expensive? false]
[ set expensive? true ]
有人知道更好的方法吗?
Anyone know a better way?
推荐答案
一些选项:
-
one-of [ true false ]
random 2 = 1
random-float 1 < 0.5
-如果需要修改概率,以获取所需的任何伯努利分布
random-float 1 < 0.5
- If you need to modify the probability, to get any Bernoulli distribution you want
如果我在模型中处理了很多概率问题,我喜欢添加
If I deal with a lot of probabilistic stuff in a model, I like to add
to-report probability [ p ]
report random-float 1 < p
end
作为简单的速记.
此外,请注意,ifelse
在您的代码中是多余的.您可以执行set expensive? one-of [ true false ]
或任何首选方法.
Also, note that the ifelse
is redundant in your code. You can just do set expensive? one-of [ true false ]
or whichever is your preferred method.
这篇关于如何在Netlogo中创建随机的二进制/布尔变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!