one-of

 

one-of reports one randomly selected member from a provided agentset or list. For example, if we were creating a forest fire model in which a fire broke out at a random patch, we would write the following code:

create-trees 100 [
    set shape "tree"
    set color green
    move-to one-of patches
]
ask one-of trees [
    set color red
]

Things to keep in mind when using one-of:

In the model example below, we have a small population of blue turtles representing healthy people. We use one-of to turn one randomly picked turtle red, which indicates an infected agent (i.e., patient zero).

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

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

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

Reports "n" randomly picked agents from an agentset or items from a list.

Read more
random

Reports a random whole number between 0 and a specified number.

Read more
max-n-of

Reports a list of top values in a provided list.

Read more
let

Creates a local variable that only exists within a procedure or a statement surrounded with brackets (`[ ]`).

Read more
 
Learn another primitive: