Marc
Marc

Reputation: 71

NETLOGO - Option with a certain probability

I try to make my first ABM using NETLOGO. I would like to show in an easy way how an election works.

So I create 3 types of turtles : young people, adults and senior (I create this because of candidate's preferences are not the same, one's are more social than others more liberal...).

So I would like to make them move, and changing the p-color of the patch into they move with probability. For example in 2012 young people vote (in a simply way) 30% social (color 136), 30% liberal (color 97), 20% extreme-right (color 104) and 10% extreme-left (red).

So, I would like in my code to introduce probability when turtles move to patches and change color.

This is the interesting part of my code :

to chose-color-young

ask jeunes

[if pcolor = white [set pcolor one-of [136 97 104 15]]]

end

I would like to do something like that 136 with probability = 0.3 ;97 with probability = 0.3 ; 104 with probability = 0.20 and 15 with probability = 0.10.

Upvotes: 0

Views: 1081

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

The rnd extension does exactly what you need:

let probs [[136 0.3] [97 0.3] [104 0.20] [15 0.10]]
ask jeunes [
  if pcolor = white [
    set pcolor first rnd:weighted-one-of-list probs last
  ]
]

See this other answer for more explanations on the rnd extension.

Upvotes: 2

Related Questions