How to create random binary/boolean variable in Netlogo

跟風遠走 提交于 2019-12-04 19:26:45
Bryan Head

A few options:

  • one-of [ true false ]

  • random 2 = 1

  • 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

as an easy shorthand.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!