Netlogo: Assign variable using probabilities

后端 未结 2 1943
北恋
北恋 2020-12-17 01:16

How to assign a string or integer variable to turtle, using probabilities of the variables in a group/list? For example it is 0.4 probability that one specific variable is u

2条回答
  •  执念已碎
    2020-12-17 01:35

    Don't overlook the rnd extension: https://github.com/NetLogo/Rnd-Extension But it is possible to do it essentially as you propose. I'll to that here, but it would be better to use explicit arguments.

    to-report random-pick
      let _r random-float 1
      let _ps [0.1 0.2 0.4 0.3]
      let _lst ["String1" "String2" "String3" "String4"]
      let _i 0
      while [_r >= item _i _ps] [
       set _r (_r - item _i _ps)
       set _i (_i + 1) ]
      report item _i _lst
    end
    

提交回复
热议问题