mod
is a mathematics primitive that completes the modulo operation, which takes two numbers, divides them, and returns the remainder. For example, 17 mod 7
would report 3 because $17 = 2 * 7 + 3$.
mod
is very useful in some interesting NetLogo modeling applications such as creating grids. For example, the following code would create a checkerboard pattern like a chess board:
ask patches [
if (pxcor + pycor) mod 2 = 1 [
set pcolor white
]
]
Things to keep in mind when using mod
:
mod
works with floating point numbers, too. For example, 5 mod 1.5
would report 0.5.mod
with negative numbers may be counterintuitive. For example, while 5 mod 3
would report 2, 5 mod -3
would report -1.In the model example below, we use mod
to create a maze in which 3 mice compete for the pieces of food scattered around randomly. Without mod
, creating this maze would be extremely tedious.
xxxxxxxxxx
breed [mice mouse]
breed [foods food]
to setup
clear-all
ask patches with [pxcor mod 3 = 0 or pycor mod 3 = 0] [
set pcolor gray
]
create-mice 3 [
set shape "mouse"
set color brown
move-to one-of patches with [pcolor = gray]
]
create-foods 20 [
set color yellow
set size 0.5
move-to one-of patches with [pcolor = gray]
]
reset-ticks
end
to go
ask mice [
wiggle
ifelse [pcolor] of patch-ahead 0.5 = gray [
forward 0.5
][
set heading one-of [0 90 180 270]
]
if any? foods-here [
ask foods-here [ die ]
]
]
if count foods = 0 [ stop ]
tick
end
to wiggle
left random 30
right random 30
end
Once you mastered the mod
primitive, don't stop there. Check out the resources below to improve your NetLogo skills.
mod
primitive:with
Reports a subset of the original agentset that only contains the agents with specified characteristics.
random-float
Reports a random number with decimal points between 0 and a specified number.
random-normal
Reports a random number with decimal points that is picked from over a normal distribution with a specified mean and standard deviation.