如果我在 NetLogo 上运行此代码几次,有时会得到一个绿色和两个红色,有时会得到三个红色,这是一个错误还是代码有问题?
breed [greens a-green]
breed [ reds a-red]
to setup
clear-all
create-greens 1
ask greens [ set color green setxy random-xcor random-ycor]
create-reds 2
ask reds [ set color red setxy random-xcor random-ycor]
reset-ticks
end
您的代码似乎没有任何问题 - 我只是运行了一个稍微简化的版本 10000 次,除了 1 个绿色和 2 个红色之外,没有任何其他计数的情况。尝试这个稍微修改过的设置版本:
breed [greens a-green]
breed [reds a-red]
globals [ green-count red-count ]
to setup
create-greens 1 [
set color green
setxy random-xcor random-ycor
]
create-reds 2 [
set color red
setxy random-xcor random-ycor
]
end
运行几次并跟踪每种颜色的计数:
to colors-check
ca
set green-count []
set red-count []
repeat 10000 [
ask turtles [ die ]
setup
set green-count lput count turtles with [ color = green ] green-count
set red-count lput count turtles with [ color = red ] red-count
]
print word "Green counts recorded: " remove-duplicates green-count
print word "Red counts recorded: " remove-duplicates red-count
reset-ticks
end
我的输出是:
Green counts recorded: [1]
Red counts recorded: [2]
这告诉我,对于所有这 10000 次重复,颜色计数均符合预期(否则在删除重复项时您会看到其他数字)。也许在设置后运行了一些其他命令?