linda
linda

Reputation: 117

Set probability range for heading in Netlogo

My problem consists of 2 things:

  1. how to code 4 different range for heading

    • a. 315 - 45
    • b. 46 - 135
    • c. 136 - 225
    • d. 226- 314

2.how to code probability on choosing this range. (I have attached image below to make it clear)

What I wish to do is:

a) for heading a; the probability of turtles choose any of this range should be 85%

b) for heading b,c, and b, there will be 5% of turtles choosing any side of this heading

Since I'm quite bad at explaining in words, I have attached image on what I plan to do

Right-click and open in new tab

here is my code

to random-behave

  let p random-float 100

  if (p >= 85)
  [set  heading heading + 45
    set heading heading - 45]

  if (p >= 15)
   [set  heading other heading + 45
    set heading other heading - 45]

    if not can-move? 1 [ rt 180 ]

   fd 1
  ]
end

I tried to implement rnd extension but I can't see where should I put it. And if possible, instead of if (p >=15), is there any way to code the heading according to its degree? Since other can't be use here.

Thank you in advance.

Upvotes: 0

Views: 210

Answers (1)

JenB
JenB

Reputation: 17678

You don't need the rnd extension, your approach with random-float is fine. However, you need to use if-else rather than if because a returned random number 'p' that is >85 will also be >15. Instead, you need to break up the interval from 0 to 100 into pieces that are the appropriate length. This is not tested.

to random-behave

  let p random-float 100

  if-else p <= 85
  [ print "got up to 85" ]
  [ if-else p <= 90
    [ print "got 85 to 90"]
    [ if-else p <= 95
      [ print "got 90 to 95" ]
      [ print "got 95 to 100" ]
    ]
  ]

  if not can-move? 1 [ rt 180 ]
  fd 1
end

You also have errors in your heading calculation. I only answered the probability part since that's what you specifically asked. But you should ask a new question if you need help with your heading.

Upvotes: 4

Related Questions