How incorporate random-float into a command? NETLOGO - if-statement

To eat ;to kill prey and eat it
let kill one-of prey-here in-radius smell
;need to code in a variable for success too
if kill != nobody
[ask kill [ die ]
set energy energy + 10000]
end
At present this is my command for my turtle to eat prey when they run into them on the map.
I want to add a variable so that there is a chance they don't get to eat. I know I need to use random-float 1
if random float is > .4 [what happens]
but i do not know where to add it into my eat commands so that it works.
can someone please advise?

I think you want if random-float 1 > .4 [do something]

Related

Netlogo Lists: detecting the patch in a list with max distance to one specific patch - for each turtle

I want to solve a problem in Netlogo that so far exceeds my programming skills. I want to built a list for each turtle which contains the furthest patch the turtle went on this "day". So far I tried to built a list were all patches are stored for each turtle. Now I want to calculate - for each turtle - the patch from this list that has maximum distance from its home (hide). I want to empty the list every night (thats not mandatory) Thats my code so far:
let temp-visited-patch-list lput patch-here temp-visited-patch-list
if period = night
[
[foreach [temp-visited-patch-list] [x -> set visited-patch-list lput (max x [distance hide]) visited-patch-list]]
let temp-visited-patch-list []
]
So I am not that far to extract the values for each turtle seperatly - and even the part I posted does not work. I get an "expected command" error. I would be very thankful for any suggestions to solve this problem.
Best regards
Olivia
This code has the turtles move, remember their furthest patch and turns those patches red and calculates average distance. It should help orient you how to use patches and turtle attributes to solve your question.
globals [home-patch]
turtles-own
[ farpatch
maxdistance
]
to setup
clear-all
set home-patch one-of patches
ask home-patch
[ set pcolor blue
sprout 20
]
reset-ticks
end
to go
repeat 20 [movement]
ask (patch-set [farpatch] of turtles) [set pcolor red]
type "Average max distance:" print mean [maxdistance] of turtles
tick
end
to movement
ask turtles
[ set heading random 360
forward 1
if distance home-patch > maxdistance
[ set maxdistance distance home-patch
set farpatch patch-here
]
]
end

Netlogo: How can I ask the specific patch changes the color (black) only for a specific ticks with the timing of Poisson distribution?

How can I ask the specific patch changes the color (black) only for a specific ticks with the timing of Poisson distribution? I am a beginner of Netlogo. The following is a sample program. But in this program the patch has been white colored all the time. Thank you.
ask patch max-pxcor 0 [
set poisson poisson - 1
if poisson < 0 [ ;I have no idea of the good condition setting.
set poisson random-poisson (stop-ticks)
set pcolor black
]
set pcolor white
]
Try this. It creates a counter (conveniently called counter) that decreases each tick and when it hits 1 (you might need 0) turns the patch black then resets the patch to white with a new counter then next tick. The poisson distribution here has a mean of 5, but that can be changed.
globals [counter]
to setup
clear-all
ask patches [set pcolor white]
set counter random-poisson 5
reset-ticks
end
to go
print counter
if-else counter = 1
[ ask patch max-pxcor 0 [set pcolor black]
set counter random-poisson 5
]
[ ask patch max-pxcor 0 [set pcolor white]
set counter counter - 1
]
tick
end

Netlogo: How can I add some conditions by using the with, if statement or else?

I would like to count the number of turtles stopped on the road, and I want to take that X coordinate information and let it be the queue length. The following is the sample program.
ask turtles with [ not right-end ] ;a flag "right-end" to the red turtle for differentiation to the other blue turtles
[
ask turtles with [ speed = 0 ] ;the speed is 0 means stopped
[
set top max-one-of turtles [who] ;get a turtle with biggest id
set topx [xcor] of top
set L count turtles with [xcor > topx] ; L is the queue length of Little's Law
]
]
I am having trouble understanding exactly what you want to do, but I think it is something like this:
to-report countRightmost ;global context
let ts (turtles with [speed = 0 and not right-end] )
let top max-one-of turtles [who]
let topx [xcor] of top
report count ts with [xcor > topx]
end

Error using "n-of" in NetLogo to select specific patches

I am new to NetLogo and I am working on a deforestation model, where farmers are my agents. They have a ID variable (lotid-farmer) that matches a ID in the patches (lotid-patch) that they own. Basically, farmers check how much money and labor they have to deforest some forest patches or abandon some of their agricultural patches (pcolor = red) in their farms. By the end of the latter procedure, farmers have a "n-aband" value calculated, which is the number of patched they will abandon (randomly) within their farms (pcolor = yellow). This is the piece of code that was supposed to do that:
ask farmers [
...
if pcolor = red and lotid-patch = [ lotid-farmer ] of self [ ; Asks the farmer to randomly convert a # of the agricultural patches without maintenance to regrowth...
ask n-of n-aband patches [ set pcolor yellow ] ; ... among all agricultural patches own by the farmer
]
...
However, when I run the code, farmers start "abandoning" patches that they do not own. I think that is because I am not setting up a proper "restriction" when I use "ask n-of n-aband patches" but I thought that that should be implicit in the "if" statement I have in the line above, no? I have also tried:
ask n-of n-aband patches with [ pcolor = red and lotid-patch = [ lotid-farmer ] of self ] [ set pcolor yellow ]
But I run into a error:
A patch can't access a turtle variable without specifying which turtle.
error while patch 390 414 running OF
called by procedure CALCULATE-DEFORESTATION
called by procedure GO
called by Button 'Go'
Any suggestions on how to get this piece working? Thank you in advance.
To do this, you need to consider only patches owned by the farmer.
ask farmers [
let _id lotid-farmer
let _candidates (patches with [lotid-patch = _id and pcolor = red])
let _n-aband min (list n-aband count _candidates)
ask n-of _n-aband _candidates [set pcolor yellow]
]
But ... don't work with ids. Work with agents. Let each patch have an owner,
which is a farmer or nobody. Let each farmer maintain a list (or agentset)
of patches that it owns.

netlogo comparing turtle variables

I'm writing a program which groups turtles into different groups.
I am trying to write a command where if there is a turtle in a different group within a 1 until radius of the turtle the command runs.
This is what I have
to confront
ask turtles
[if [ group ] of turtles in-cone 1 180 != group
[set color brown]]
end
However the command is coming out true even when there is not a turtle in a different group nearby.
may be something like :
to confront
ask turtles
if any? turtles in radius 2 [
if any? turtles in radius 2 with [group != [group] of myself][
set color brown
]
]
end
I don't know if it can work directly (I haven't test this code) but I think it's a way