Search code examples
netlogoflags

NetLogo: How to alternate specific flags immediately after the birth of the turtle?


How to alternate specific flags immediately after the birth of the turtle?

How do I alternately flag a turtle created with an origin cell (0 0) separately? For example, the first turtle to be born is flag 1, the next turtle to be born is flag 2, the next turtle to be born after that is flag 1, and the next turtle to be born after that is flag 2...and so on. (e.g., ID number 1 = flag 1, ID number 2 =flag 2, ID number 3 = flag 1, ID number 4 =flag 2, ID number 5 = flag 1, ID number 6 =flag 2, ...)

I've written the sample code below, but it's not yet finished and no ideas :

globals [ idnumber flag1 flag2 ]

to setup
  clear-all
  reset-ticks
  set idnumber who
end

to go

  ask patch 0 0 [
    sprout 1
  ;Perhaps we could put a if, or ifelse conditional statement here to construct the flag1 and flag2 
   flagging syntax?
  ]

end

Solution

  • If you want to strictly alternate (and there's no other turtles being created in between) then the easiest thing to do is use mod:

    ifelse who mod 2 = 0 [set flag 1][set flag 2]

    This could also be set flag1 true or whatever code you like, the key point is mod.

    If you just want to randomly choose between them with equal probability, then one-of may be useful:

    set flag one-of [1 2]