Lottery Example
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
WHAT IS IT?
NetLogo makes it easy to make a random choice where every outcome is equally likely. But what if you want different outcomes to have different chances of being chosen...?
In this example, we show how to run a "lottery" in which we pick a random turtle, and a turtle's chance of winning is proportional to its size.
EXTENDING THE MODEL
In this version of the code, the agentset for the lottery (turtles
) and the reporter run by each agent (size
) are both hardcoded in the lottery-winner
procedure. If those were passed as inputs to the procedure instead, then the same procedure could be used to run different lotteries. This would be possible using reporter tasks. (See the Tasks section of the Programming Guide.)
RELATED MODELS
Preferential Attachment, in the Networks section of Sample Models, uses this code.
Comments and Questions
to setup clear-all ;; create a turtle on every fifth patch ask patches with [pxcor mod 5 = 0 and pycor mod 5 = 0] [ sprout 1 [ ;; vary the size of the turtles set size 2 + random 6 ;; start them out with no wins set label 0 ;; make turtles darker so the labels stand out set color color - 2 ] ] reset-ticks end to go ask lottery-winner [ set label label + 1 ] tick end ;; The idea behind this procedure is a bit tricky to understand. ;; Basically we take the sum of the sizes of the turtles, and ;; that's how many "tickets" we have in our lottery. Then we pick ;; a random "ticket" (a random number). Then we step through the ;; turtles to figure out which turtle holds that ticket. to-report lottery-winner let pick random-float sum [size] of turtles let winner nobody ask turtles [ ;; if there's no winner yet... if winner = nobody [ ifelse size > pick [ set winner self ] [ set pick pick - size ] ] ] report winner end ; Public Domain: ; To the extent possible under law, Uri Wilensky has waived all ; copyright and related or neighboring rights to this model.
There are 11 versions of this model.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
Lottery Example.png | preview | Preview for 'Lottery Example' | over 12 years ago, by Uri Wilensky | Download |
This model does not have any ancestors.
This model does not have any descendants.