n-of

 

n-of is used when we want to randomly select a specific number of elements out of an agent set. It is similar to how one-of works. For example, if we wanted to create a model in which we wanted to have 90 yellow birds and 10 red birds, we would write the following code instead of using create-turtles twice:

create-turtles 100 [
    set shape "bird"
    set color yellow
    setxy random-xcor random-ycor
]
ask n-of 10 turtles [
    set color red
]

Things to keep in mind when using n-of:

In the model example below, we use n-of to pick 50 random patches to sprout 50 plants. We also create 5 cows to graze in this grassland. We also use n-of to make two of our cows violet.

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

Once you mastered the n-of primitive, don't stop there. Check out the resources below to improve your NetLogo skills.

 
Published NetLogo models that use the n-of primitive:
 
 
Similar primitives:
one-of

Reports one randomly picked member from a provided agentset or list.

Read more
count

Counts the number of agents in an agentset.

Read more
any?

Checks if there is at least one agent in an agentset.

Read more
all?

Checks if a reporter is true for all the agents in an agentset.

Read more
 
Learn another primitive: