NetLogo: How do I make sure a variable stays in a certain range?

I have several variables that can be inherited by child agents with a change of + 0.1 and -0.1, either without any changes or randomly. What I did is as follows: (The code is just an example)

to reproduce
  ask turtle 1
  [
  let X-Of-Mother X

  hatch 1
  [

    set X one-of (list (X-Of-Mother) (X-Of-Mother + 0.1) (X-Of-Mother - 0.1) (random-float 1))

    ]

  ]

end

Currently, I have to check if X of the baby turtle is always in range:

if X > 1 [set X X - 0.2]
if X < 0 [set X X + 0.2]

What could be the best way to do this?

What if I have to use random-normal 0.5 0.1, how can I limit it to values ​​from 0 to 1, I have done many repetitions of generating such random numbers, I think that the quality of random normal is good, and there isn’t so many times that I need to check if it is out of range.

eg:

  to test 
    Let c 0
    let b 0 
    repeat 100000000
    [Set b random-normal 0.5 0.1
      if b > 1 [set C C + 1]
      If b < 0 [set C C + 1]

      ]
    print c  

  end

* 67 100000000 * 67 , , 58, 51,...

+4
3

, random-normal , , , .

random-normal , . , :

observer> clear-plot set-plot-pen-interval 0.01 set-plot-x-range -0.1 1.1
observer> histogram n-values 1000000 [ median (list 0 (random-normal 0.5 0.2) 1) ]

enter image description here

, Marzy , , random-normal , . :

to-report random-normal-in-bounds [mid dev mmin mmax]
  let result random-normal mid dev
  if result < mmin or result > mmax
    [ report random-normal-in-bounds mid dev mmin mmax ]
  report result
end

observer> clear-plot set-plot-pen-interval 0.01 set-plot-x-range -0.1 1.1
observer> histogram n-values 1000000 [ random-normal-in-bounds 0.5 0.2 0 1 ]

enter image description here

- , , . , random-float:

observer> clear-plot set-plot-pen-interval 0.01 set-plot-x-range 0 1
observer> histogram n-values 10000000 [ 0.5 + random-float 0.5 - random-float 0.5 ]

histogram

+8

:

set x median (list 0 (y) 1)

y - ( ), 0 - , 1 - .

, y , 1, 1. y 0, 0. y.

, , [0, 1]:

 to test
    let b median (list 0 (random-normal 0.5 0.1) 1)
    print b
 end
+3

, ...

, , , , , - . , 3,3 4,4, , ( 0 1, , , /, , ).

Netlogo -, , :

to-report random-beta [ #shape1 #shape2 ]

  let Xa random-gamma #shape1 1
  let Xb random-gamma #shape2 1
  report Xa / (Xa + Xb)

end

. https://math.stackexchange.com/questions/190670/how-exactly-are-the-beta-and-gamma-distributions-related

0

Source: https://habr.com/ru/post/1529053/


All Articles