本文介绍了将每个数字分配给3只海龟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个有1500个学科"的世界中,我正在创建一个程序,该程序可以创建1500个科学家"(海龟).

I am creating a program that creates 1,500 "scientists" (turtles), in a world where there are 1500 "disciplines".

我需要为每只海龟分配一个纪律",编号范围为1-500,并确保每个学科有3只海龟.

I need to assign each turtle a "discipline" as a number between 1-500, and ensure that there are 3 turtles for each discipline.

这意味着设置(随机)不合适.有没有可以使用的原语?

This means that set(random) isn't appropriate. Is there a primitive I can use?

没关系,我想我已经弄明白了.这有意义吗?

Nevermind, I think I have figured it out. Does this make sense?

  to set-discipline
    ask turtles [ set discipline -1 ]
    let unassigned turtles
    let current 1
    while [any? unassigned]
    [
      ask n-of (min (list group-size (count unassigned))) unassigned
        [ set discipline current ]
      set current current + 1
      set unassigned unassigned with [discipline = -1]
    ]
  end

推荐答案

尼古拉斯所说的话.但也请考虑以下替代方法:

What Nicolas said. But consider also this alternate approach:

to test
  let disciplines reduce sentence map [(list ? ? ?)] n-values 500 [? + 1]
  ask turtles [
    set discipline first disciplines
    set disciplines butfirst disciplines
  ]
end

ask是随机顺序的,因此我们不需要对disciplines列表进行混洗.

ask is in random order, so we don't need to shuffle the disciplines list.

或者,如果海龟尚不存在,并且您可以随意创建它们:

Or, if the turtles don't exist yet and you're free to create them as you go:

to test
  let disciplines reduce sentence map [(list ? ? ?)] n-values 500 [? + 1]
  foreach shuffle disciplines [
    create-turtles 1 [ set discipline ? ]
  ]
end

在这一本书中,由于乌龟是按照谁的编号顺序创建的,因此我们必须重新整理学科列表.

In this one, we have to shuffle the list of disciplines, since the turtles are being created in who number order.

这篇关于将每个数字分配给3只海龟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 20:12