NetLogo : How to make sure a variable stays in a defined range?

前端 未结 3 700
盖世英雄少女心
盖世英雄少女心 2020-12-06 03:29

I have a few variables which can be inherited to child agents by a variation of + 0.1 and -0.1 or without any changes, or random again, What I have done is like this: (The

3条回答
  •  悲哀的现实
    2020-12-06 03:54

    My favorite trick is this:

    set x median (list 0 (y) 1)
    

    Where y is the random number (or put in an expression), 0 is the minimum, and 1 is the maximum.

    It works because if y is greater than 1, then the median will be 1. If y is less than 0, then the median will be 0. Otherwise the median is y.

    For example, here is the random number in your example clamped to the range [0, 1]:

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

提交回复
热议问题