'Turtle distribution

How to create a set of turtles with the rising distribution of their location from the edge of the environment to the middle?



Solution 1:[1]

You can use something like this in setup

let center-x (max-pxcor + min-pxcor) / 2
let center-y (max-pycor + min-pycor) / 2
let std-dev 5  ; change this to vary how clumped the turtles are

crt 100
[
  set xcor random-normal center-x std-dev
  set ycor random-normal center-y std-dev
]

That will work if you have world-wrapping on. If world-wrapping is off, you would have to add some code to check that the values of xcor and ycor from random-normal are within the world (e.g., the turtle's new xcor is between min-pxcor and max-pxcor) -- otherwise, the code will sometimes try to put the new turtle outside the space, which is an error.

You could also use a triangular distribution that varies the density of turtles linearly from a peak at the center of the space to zero at the edge.

let center-x (max-pxcor + min-pxcor) / 2
let center-y (max-pycor + min-pycor) / 2

crt 100
[
  set xcor random-triangular min-pxcor center-x max-pxcor
  set ycor random-triangular min-pycor center-y max-pycor
]

NetLogo does not have this triangular distribution built-in, so you need to add this procedure to your code:

to-report random-triangular [a-min a-mode a-max]
  ; Return a random value from a triangular distribution
  ; Method from https://en.wikipedia.org/wiki/Triangular_distribution#Generating_Triangular-distributed_random_variates
  ; Obtained 2015-11-27

  if (a-min > a-mode) or (a-mode > a-max) or (a-min >= a-max)
   [ error (word "Random-triangular received illegal parameters (min, mode, max): " a-min " " a-mode " " a-max) ]

  let a-rand random-float 1.0
  let F (a-mode - a-min) / (a-max - a-min)

  ifelse a-rand < F
  [ report a-min + sqrt (a-rand * (a-max - a-min) * (a-mode - a-min)) ]
  [ report a-max - sqrt ((1 - a-rand) * (a-max - a-min) * (a-max - a-mode)) ]

end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steve Railsback