2

I have two procedures for turtles but I need a random number of turtles to do one of the procedures and another number of random turtles to do the other procedure in equivalent parts.

So suppose i have a population of 40 turtles and I have the following procedures:

to move
 ask turtles [
 rt random-float 90 - random-float 90
 fd 1
 ]
end

 to bounce 
ask turtles 
[
if [pcolor] of patch-at dx 400 = white [
  set heading (- heading)
 ]
 if [pcolor] of patch-at 400 dy = white [
  set heading (180 - heading)
]
]
end 

What I want to do is to have 20 random turtles do the procedure "to bounce" and the other 20 random turtles do the procedure "to move". Or the 40 turtles will do any of the 2 procedures randomly but without any turtle doing the 2 processes at once.

I know this is kind of difficult but I think it can be done. Any suggestions?

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
Paul
  • 189
  • 6
  • Can you clarify the alternative? "Or the 40 turtles do any of the 2 procedures randomly but without any turtle do the 2 processes at once." Do you mean that it is acceptable if each of the 40 turtles performs one and only one of the two procedures? – rabbit Sep 01 '15 at 04:09
  • yeah that also can be but preferably i am looking for an equivalent proportion of turtles doing the 2 proces – Paul Sep 01 '15 at 14:21

2 Answers2

2

This is the easiest way I can think of:

let bouncers n-of 20 turtles
ask turtles [
  ifelse member? self bouncers
    [ bounce ]
    [ move ]
]
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
2

There's lots of interpretations of the phrase random number of turtles. Seth's method allows you to control exactly how many turtles do each of the two procedures. That is, a fixed number of random turtles.

If you want it always to be approximately, but not necessarily equal to, some proportion of turtles (0.4 in the example below), so each turtle randomly decides with a given probability, then you can do:

ask turtles [
ifelse-value random-float 1 < 0.4
    [ bounce ]
    [ move ]
]

If you want the number of turtles to be random, then you can change the first line of Seth's code to:

let bouncers n-of (1 + random count turtles) turtles

Note: none of this code is tested so please comment (or edit) if it throws an error.

JenB
  • 17,620
  • 2
  • 17
  • 45