2

如何随机选择 3 个程序之一?

请看下面:

to move
   ifelse random-float 100 < 70 
   [move-forward]
   [move-left move-right move-back] ;; To randomly select one of these 3 procedures to execute.
end

谢谢你。我对语法很陌生。

4

2 回答 2

8

网络徽标 6:

run one-of (list [-> move-left]
                 [-> move-right]
                 [-> move-back])

网络徽标 5:

run one-of (list task move-left
                 task move-right
                 task move-back)
于 2012-03-12T16:29:59.107 回答
3

也许不是太优雅,但这可能有效:

to move
  ifelse random-float 100 < 70 
  [move-forward]
  [let n random 3
    ifelse n = 0
    [move-left]
    [ifelse n = 1
      [move-right]
      [move-back]]]
end
于 2012-03-11T22:05:24.203 回答