我有一个值列表,我希望模型中的代理可以从这些列表中抽样而不进行替换。 n-of
原语允许我随机采样,但这意味着有时值会重复,这是我要避免的事情。
例如,如果agent-1从列表1中获得1,agent-2也将无法获得该值。
希望您能提供帮助。
turtles-own [list1Vals list2Vals]
to test
clear-all
crt 2
let list1 [1 2]
let list2 [3 4]
ask turtles [set xcor random-xcor
set ycor random-ycor
set color red
set list1Vals n-of 1 list1
set list2Vals n-of 1 list2
]
end
答案 0 :(得分:2)
随机选择索引值而不是从列表中随机选择最容易,因为然后您可以使用item
选择列表值,然后使用remove-item
从原始列表中删除它。评论中的更多详细信息:
turtles-own [list1Vals list2Vals]
to test
ca
let list1 [1 2 3 4 5]
crt 5 [
; Randomly choose an index based on the
; length of list1
let ind1 one-of range length list1
; Have the turtle choose from list1
; using that index
set list1Vals item ind1 list1
; Remove the indexed value from list1
set list1 remove-item ind1 list1
show ( word "I chose " list1Vals ". list1 is now: " list1 )
]
reset-ticks
end
test
输出类似:
(turtle 1): "I chose 5. list1 is now: [1 2 3 4]"
(turtle 4): "I chose 4. list1 is now: [1 2 3]"
(turtle 0): "I chose 3. list1 is now: [1 2]"
(turtle 2): "I chose 2. list1 is now: [1]"
(turtle 3): "I chose 1. list1 is now: []"
或
(turtle 1): "I chose 1. list1 is now: [2 3 4 5]"
(turtle 0): "I chose 4. list1 is now: [2 3 5]"
(turtle 4): "I chose 2. list1 is now: [3 5]"
(turtle 3): "I chose 5. list1 is now: [3]"
(turtle 2): "I chose 3. list1 is now: []"