NetLogo 不以随机间隔创建海龟

发布于 2024-10-16 05:48:39 字数 442 浏览 5 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情丝乱 2024-10-23 05:48:39

这里有一个想法,不要每次都重新生成新的随机数,而是在计时器到达后重新生成。代码:

globals [t]
to setup
  set t random 60
end
to go
  every t [
    set t random 60
    crt 1 [
      set xcor random 20 - 10
      set ycor random 20 - 10
    ]
  ]
end

我还没有测试过它,但它应该可以解决 Jose 提出的问题。

Here is an idea, don't keep regenerating a new random number every time, regenerate it after the timer is reached. Code:

globals [t]
to setup
  set t random 60
end
to go
  every t [
    set t random 60
    crt 1 [
      set xcor random 20 - 10
      set ycor random 20 - 10
    ]
  ]
end

I haven't tested it, but it should address the issue that Jose brought up.

如痴如狂 2024-10-23 05:48:39

你的第一段代码正在做它应该做的事情,即几乎每时每刻都创建一只乌龟。

这可能不太直观,但请注意“go”每秒被调用数千次(取决于您的机器速度)。每次调用它时,它都会生成一个 0 到 60 之间的新随机数。因此,它生成数字 0 的可能性非常高。如果是这样,那么它会在此时创建一​​只乌龟。

例如,从“永远”按钮运行此代码并查看它打印出的内容:

to go
  every random 60 [
    show timer ;shows how long its been since last reset-timer
    reset-timer
  ]
end

我得到:

observer: 0.016
observer: 0.0060
observer: 0.016
observer: 0
observer: 0
observer: 0
observer: 0.0050
observer: 0
observer: 0
observer: 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:

to go
  every random 60 [
    show timer ;shows how long its been since last reset-timer
    reset-timer
  ]
end

I get:

observer: 0.016
observer: 0.0060
observer: 0.016
observer: 0
observer: 0
observer: 0
observer: 0.0050
observer: 0
observer: 0
observer: 0

on my laptop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文