Search code examples
randomanylogic

Random choice in Anylogic


I want to select a random number from [0,1,2,3] in a probability of [0.1, 0.2, 0.3, 0.4] in anylogic. This can be easily done in Python by using numpy.random.choice. However, I couldn't find someway to do this in anylogic. I don't want to use the customized distribution since I want to apply it to many agents in different parameters.

I am looking for someway to do this in anylogic.


Solution

  • you can do this with customDistributions

    cd

    YOu can just get the value by calling customDistribution()

    If you want to do it in a more flexible way: Create a variable called cd in your agent of type CustomDistribution and then you can do something like this:

    int[]x={0,1,2,3,4};//you need an extra number to complete the interval
    double[]y={0.1,0.2,0.3,0.4,0};//extra number has probability 0
    cd=new CustomDistribution(x,y,new Random());
    

    to get the random value you do

    roundToInt(cd.get(new Random()));