How to randomly select two patches from neighbors4 in NetLogo

I am trying to select 2 neighbors at random, for example, we have the option to select 8 neighbors from the "neighbors" command, similar to selecting 4 neighbors, we use the command neighbors4

, but what if we want to randomly select only 2 neighbors out of 8 neighbors.

I want to do something like this:

ask n-of 2 patches from neighbors with [pcolor = white]
  [set pcolor blue]

      

but since there is no command from

so I don't know how it will work.

+3


source to share


2 answers


You were very close! Do you want to:

n-of 2 neighbors with [pcolor = white]

      



But what if there are possibly fewer than two neighbors who are white? In this case, I suggest using the reporter at-most-n-of

defined in this answer
.

+4


source


You basically solved this:

neighbors

(or in your case neighbors with [condition]

) returns the agent that is expecting n-of

, so all you need is



[
 ask n-of 2 neighbors with [pcolor = white] [ set pcolor blue ]
]

      

...

+3


source







All Articles