Reputation: 73
I'm trying to modify a previous Netlogo Predator-Prey model to suit my purposes. The trouble I'm running into is that I want to control the probability of the predator killing the prey when there is an encounter and I'm not sure how to go about it. I know I need a slider on the interface side and I have that ('kill-probability') but getting it into the code all together is another matter.
Here is the section of the model I'm trying to adapt:
to catch-sheep ;; wolf procedure
let prey one-of sheep-here ;; grab a random sheep
if prey != nobody ;; did we get one? if so,
[ ask prey [ die ] ;; kill it
set energy energy + wolf-gain-from-food ] ;; get energy from eating
end
Any and all help would be appreciated.
Upvotes: 0
Views: 235
Reputation: 17678
The logic here is to get a random number in the interval [0,1] (or in [0,100] if you think in percentages) and, if the random number is less than your slider value, then do the action. The slider controls the probability. So, using your slider name of 'kill-probability' and assuming it runs from 0 to 1, modify your code like this:
to catch-sheep
let prey one-of sheep-here
if prey != nobody and random-float 1 < kill-probability ; this is the line I changed
[ ask prey [ die ]
set energy energy + wolf-gain-from-food ]
end
If your slider is a percentage, use random 100 < kill-probability
instead
Upvotes: 1