如何在 Netlogo 中创建随机 binary/boolean 变量

How to create random binary/boolean variable in Netlogo

我想为每只海龟分配一个随机布尔变量,但我没有看到可以模拟伯努利分布抽签的函数。

这很接近,但很尴尬:

ifelse random-in-range 0 1 < .5 [set expensive? false]
[ set expensive? true ]

有人知道更好的方法吗?

几个选项:

  • one-of [ true false ]

  • random 2 = 1

  • random-float 1 < 0.5 - 如果你需要修改概率,得到你想要的任何伯努利分布

如果我在模型中处理很多概率性的东西,我喜欢添加

to-report probability [ p ]
  report random-float 1 < p
end

作为一个简单的 shorthand.

另外,请注意 ifelse 在您的代码中是多余的。您可以只执行 set expensive? one-of [ true false ] 或您喜欢的任何一种方法。