random

 

random is a primitive that allows us to add randomness into our models. When we provide a number to random, it will report a random number from 0 to N-1. Imagine that each time you run the primitive random N, you are rolling a die with N number of sides. For example, if we wanted to create a forest fire in which we assigned each patch to be either a tree (pcolor = green) or ground (pcolor = brown) but with 70 percent tree density, we would write the following code:

ask patches [
    ifelse random 100 < 70 [
        set pcolor green
    ][
        set pcolor brown
    ]
]

Things to keep in mind when using random:

In the model example below, we have 6 turtles that represent the 6 faces of a dice. Every time we click the roll dice button, each turtle picks a random number between 1 to 6 (including 6) and updates its shape accordingly.

Why do we need to add 1 to value we give to random to get the full range of values? In math and computer science language, we would say that the random primitive produces numbers in the range 0 to N exclusive, meaning that the number N itself is excluded from the possible set of values. In order to make it inclusive for N we have tell random to set the exclusive limit at N + 1, which gets N back in the range of possible results.

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

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

 
Published NetLogo models that use the random primitive:
 
 
Similar primitives:
random-float

Reports a random number with decimal points between 0 and a specified number.

Read more
random-normal

Reports a random number with decimal points that is picked from over a normal distribution with a specified mean and standard deviation.

Read more
globals

Defines variables that can be accessed throughout the whole model and has the same value for all the agents.

Read more
turtles-own

Declare a variable that belongs to turtles.

Read more
 
Learn another primitive: