NetLogo banner

Home
Download
Help
Forum
Resources
Extensions
FAQ
NetLogo Publications
Contact Us
Donate

Models:
Library
Community
Modeling Commons

Beginners Interactive NetLogo Dictionary (BIND)
NetLogo Dictionary

User Manuals:
Web
Printable
Chinese
Czech
Farsi / Persian
Japanese
Spanish

  Donate

NetLogo Models Library:
Code Examples/Extensions Examples/rnd

(back to the library)

Rnd Example

[screen shot]

Note: If you download the NetLogo application, every model in the Models Library is included.

WHAT IS IT?

This example demonstrates the use of the rnd extension to do weighted random selection.

HOW IT WORKS

We create as many turtles as there are base colors in NetLogo (i.e., 14) and align them horizontally on the screen. Initially, all turtles have size 1. At each tick we randomly pick one turtle and increase its size by 1. The model stops when the total width of the turtles is enough to horizontally fill the world.

The model offers two ways to randomly select turtles. When WEIGHTED-SELECTION? is turned off, the selection is done in a uniform fashion, using one-of turtles, and each turtle has the same probability of being picked. When WEIGHTED-SELECTION? is turned on, we use rnd:weighted-one-of turtles [ size ] instead, so bigger turtles stand a greater chance of being picked. For example, a turtle with size 2 is twice as likely to be picked as a turtle with size 1.

HOW TO USE IT

Press SETUP to initialize the model and GO to run it.

The WEIGHTED-SELECTION? switch control whether or not the random selection is proportional to the size of the turtle.

The TURTLE SIZES bar plot shows the size of each turtle, in decreasing order.

THINGS TO NOTICE

Try a few runs with WEIGHTED-SELECTION? turned on, and then a few runs where it's turned off. Can you notice the change in the distribution of sizes in the TURTLE SIZES plot?

RELATED MODELS

The Lottery Example model achieves a very similar thing using only regular NetLogo primitives. Can you think of a way to rewrite the Lottery Example model using the rnd extension?

(back to the NetLogo Models Library)