I need to set a turtle variable in a range of values randomly, the range must go from 800 to 1000.
I wrote this code:
turtles-own [income]
to setup
create-turtles 100 [
set income range 800 1000
]
end
The code is not working.
I tried ramdom-float but the values range from 0 to max value.
range returns a list of numbers between the start and stop, where you want to choose a number from a range---these are different.
I didn't find a random-range or a random-uniform, so these are some approaches:
You can do a random number between 0 -200 and just add 800 to scale to your range.
turtles-own [income]
to setup
create-turtles 100 [
set income (random 200) + 800
]
end
Alternatively, if you want to create a list using the range function and choosing one of them.
turtles-own [income]
to setup
create-turtles 100 [
set income one-of (range 800 1000)
]
end
Related
i am struggling with a way to find the turtle with the most neigbors in a radius of 3 and change its color. At the moment i tried something with a while loop that increases an the id of the turtles and tests if the number of neighbors is higher than the last one. But It's causing anf infinite loop and travels only between the id 0 and 1.
I can't seem to find where the error comes from, Here's the code i wrote:
to election
while [var1 < 9][
ask turtle var1 [
set voisin count turtles in-radius 3
if voisin > maxi [
set maxi voisin
set idmax var1
]
show voisin
show idmax
set var1 var1 + 1
set color pink
]
]
ask turtle idmax[
set color green
]
end
You can do this with a single statement,
ask max-one-of turtles [count other turtles in-radius 3] [set color green]
max-one-of finds the turtle with the maximum value of the expression in brackets. (If there are multiple turtles with that maximum value, then a random one of those with the maximum value is chosen.) In the brackets, each turtle evaluates the number of other turtles in radius 3. The other is not strictly necessary. Without it, every turtle will count itself, adding one to the count.
How can I give each tick a random amount of turtles a change in a binary variable (1 or 0), whereas no more than 5 % of the existing population at all times has a value of 0 in that variable?
In other words, I wish to have that the total amount of turtles having a variable value of 0 is between 0 % or 5 % of the total amount of turtles at every tick.
How can I achieve this?
My code is:
to setup
create-turtles 100
set var random 1 (only 5 % max shall have a 0 at start)
end
to start
change
end
to change
let %draw (random 1)
if (%draw < 0) … ; than I do not how to continue
end
The n-of primitive selects the specified number of agents. You want some number up to that, so you also need to randomly generate the number. Something like this:
to setup
create-turtles 100 [ set var 1 ] ; give them all value 1
ask n-of random 6 turtles [ set var 0 ] ; randomly selects 0 to 5 turtles, assigns value 0
end
I would like to plot the sum of all turtles holding values in a range from 2 to 4. How can I achieve that?
I only get the sum of turtles holding a value of 4 with plot count turtles with [value = 4], however, I would need something like plot count turtles with [2 <= value => 4].
How can I achieve this?
My MWE is:
breed [ turtles ]
turtles-own [ value ]
to setup
clear-all
create-turtles 100
[
set value random 4
]
reset-ticks
end
to go
ask turtles [
rt random 360
fd 1
]
tick
end
NetLogo doesn't support the 2 <= value => 4 syntax. You need to write both conditions separately:
plot count turtles with [ value >= 2 and value <= 4 ]
By the way, if you wanted to plot the sum of the values instead of the count of turtles with values in that interval, you could write:
plot sum [ value ] of turtles with [ value >= 2 and value <= 4 ]
I have a very simple model of 50 turtles moving away from a central point. I would like to be able to extract the spatial coordinates (xcor, ycor) of a subset of them every nth tick in behaviour space. Hope you can help!
The modulo operator mod is probably the simplest way to do this. It outputs the remainder from a division operation, so you can just use a logical flag such that the coordinates are only extracted when ticks divided by n is equal to 0. For example:
to setup
ca
crt 10
reset-ticks
end
to go
; set up lists for example output
let tlist []
let xlist []
let ylist []
ask turtles [
rt random 60 - 30
fd 1
]
tick
; If ticks is not zero, and the remainder of
; the number of ticks / 3 is zero, extract
; some info about the turtles and print it.
if ticks > 0 and ticks mod 3 = 0 [
ask turtles with [ xcor > 0 ] [
set tlist lput self tlist
set xlist lput xcor xlist
set ylist lput ycor ylist
]
print tlist
print xlist
print ylist
]
end
Run this several times and you'll see that on tick 3 (and 6, 9, 12, etc), the lists are printed out. Note that where you have your tick increment will affect when this output is actually extracted- in the example above, tick happens at the end of the go procedure but before the if statement is evaluated.
In my model I have some agents collecting from another agent who they bump into at random after which they move back to their base. As they move back they drop off some material as defined by the random function. Here's some sample code
to go
ask searchers
[ set energy energy - 1
fd 0.0125
if random-float 1 < (1 / 50)
[ ifelse random 2 = 0
[ rt 45 ]
[ lt 45 ]
]
search
]
end
to search
if any? depots in-radius vision with [color = yellow]
[spread
set energy 0] ;; makes them to back to base
end
to spread
if random 10000 = 1 [hatch-rubbish 1 [ set color white
set shape "circle"
set size 0.5]]
end
If I set the visual radius to something enormous so they can always see the depot the number of bits of rubbish works out.
However if I allow them to move around with a radius of 1, the count of rubbish is much higher than 1 in 10,000.
Why would that make a difference?
Thanks