NetLogo 不以随机间隔创建海龟
我正在尝试在 NetLogo 中创建一个过程,以每 0-60 秒创建一个海龟代理。使用以下代码,然后循环运行该过程,随机生成器似乎无法工作。图表(代理到蜱虫)是线性的。
to go
every random 60 [crt 1 [
set xcor random 20 - 10
set ycor random 20 - 10
]
]
plot count turtles
end
但如果我这样做:
to go
every 2 [crt 1 [
set xcor random 20 - 10
set ycor random 20 - 10
]
]
plot count turtles
end
它似乎按预期工作。每 2 秒就会创建一只新海龟。
我做错了什么吗?
I'm trying to create a procedure in NetLogo to create a turtle agent every 0-60 seconds. Using the following code and then running the procedure in a loop, it appears that the random generator is not working. The graph plot (agents to ticks) is linear.
to go
every random 60 [crt 1 [
set xcor random 20 - 10
set ycor random 20 - 10
]
]
plot count turtles
end
But if I were to do:
to go
every 2 [crt 1 [
set xcor random 20 - 10
set ycor random 20 - 10
]
]
plot count turtles
end
It seems to work as expected. Every 2 seconds a new turtle gets created.
Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一个想法,不要每次都重新生成新的随机数,而是在计时器到达后重新生成。代码:
我还没有测试过它,但它应该可以解决 Jose 提出的问题。
Here is an idea, don't keep regenerating a new random number every time, regenerate it after the timer is reached. Code:
I haven't tested it, but it should address the issue that Jose brought up.
你的第一段代码正在做它应该做的事情,即几乎每时每刻都创建一只乌龟。
这可能不太直观,但请注意“go”每秒被调用数千次(取决于您的机器速度)。每次调用它时,它都会生成一个 0 到 60 之间的新随机数。因此,它生成数字 0 的可能性非常高。如果是这样,那么它会在此时创建一只乌龟。
例如,从“永远”按钮运行此代码并查看它打印出的内容:
我得到:
在我的笔记本电脑上。
Your first bit of code is doing what its supposed to, which is creating a turtle almost every instant.
This might not be intuitive but notice that the 'go' is getting called thousands of time per second (depending on your machine speed). Every time it is called it generates a new random number between 0 and 60. So, there is a really high probability that it will generate the number 0. If so, then it creates a turtle at that moment.
As an example, run this code from a 'forever' button and see what it prints out:
I get:
on my laptop.