Netlogo - 随机选择

发布于 2025-01-10 01:22:16 字数 282 浏览 3 评论 0原文

我是编程新手,非常感谢您的帮助。 在此模型中,我想创建一家公司,如果满足特定条件,可以随机选择 btw 2 个选项。最好的方法是什么? 我尝试过这个,但它不起作用。

    if profit < 0 [
set color red
let choice random 2 (
  choice = 0 [ move-to min-one-of patches [costs]] 
  choice = 1 [ set price (price + 1) ])
]

感谢您的帮助!

I am new to programming and I would really appreciate the help.
In this model, I want to create a company that can randomly choose btw 2 choices if a certain condition is met. What would be the best way to do it?
I tried this but it is not working.

    if profit < 0 [
set color red
let choice random 2 (
  choice = 0 [ move-to min-one-of patches [costs]] 
  choice = 1 [ set price (price + 1) ])
]

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

画离情绘悲伤 2025-01-17 01:22:16

看起来您需要一个 IFELSE 语句。与 IF 类似,IFELSE 评估一个测试,一个应生成 TRUE 或 FALSE 的表达式。当测试结果为 TRUE 时,IF 运行代码块。 IFELSE 有两个代码块。第一个将在测试为 TRUE 时运行,第二个将在测试为 FALSE 时运行。

如果您愿意,还可以使用两个 IF 语句。

两个 IF(顺序 IF)

let choice random 2
if (choice = 0) [ action-1 ]
if (choice = 1) [ action-2 ]

IFELSE

let choice random 2
ifelse (choice = 0)
[ action-1 ]
[ action-2 ]

请注意,这里我们只需要一个测试:(choice = 0),因为只有两个选项。如果 choice 不是 0,那么它一定是 1。

IFELSE(两个以上选项)

如果有两个以上选项,那么我们可以使用另一种形式的 ifelse,让我们可以使用多个测试。此表格必须完全包含在 ( 和 ) 中

let choice random 4
( ifelse
 ( choice = 1 ) [ action-1 ]
 ( choice = 2 ) [ action-2 ]
 ( choice = 3 ) [ action-3 ]
                [ action-4 ] ;; default
)

Looks like you need an IFELSE statement. Like IF, IFELSE evaluates a test, an expression that should produce either TRUE or FALSE. IF runs the code block when the test produces TRUE. IFELSE has two code blocks. The first will run when the test is TRUE, the second will run when the test is FALSE.

You could also use two IF statements, if you wanted to.

TWO IFs (Sequential IFs)

let choice random 2
if (choice = 0) [ action-1 ]
if (choice = 1) [ action-2 ]

IFELSE

let choice random 2
ifelse (choice = 0)
[ action-1 ]
[ action-2 ]

Note that here we only need one test: (choice = 0), because there are only two options. If choice is not 0, then it must be 1.

IFELSE (more than two options)

If there were more than two options, then we can use another form of ifelse that lets us use multiple tests. This form must be entirely wrapped in ( and )

let choice random 4
( ifelse
 ( choice = 1 ) [ action-1 ]
 ( choice = 2 ) [ action-2 ]
 ( choice = 3 ) [ action-3 ]
                [ action-4 ] ;; default
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文