Reputation: 133
On Netlogo, I want to create turtles and setxy to random-xcor and random-ycor but only on the green area that takes up the screen from under the ycor of 1.
Upvotes: 4
Views: 2754
Reputation: 12580
If your main goal is to start turtles on a random place in the green area, you can do:
create-turtles 100 [ move-to one-of patches with [ pcolor = green ] ]
one-of patches with [ pcolor = green ]
just gets a random green patch. Then, the code moves the newly created turtle to that randomly selected patch. Note that the turtles will be create on the center of the patches with this method. I recommend using this approach.
If your goal really is to set it to a random position with a maximum ycor
of 1, let's first define a function that gives us a random number from between two numbers
to-report random-between [ min-num max-num ]
report random-float (max-num - min-num) + min-num
end
Now, random-ycor
then does the same thing random-between (min-pycor - .5) (max-pycor + .5)
. The .5
s are there so that the number can be at the very top or very bottom. You can use the same technique, but just replace the max-num
with 1:
create-turtles 100 [ setxy random-xcor random-between (min-pycor - .5) 1 ]
Upvotes: 3