AnyLogic provides functions to select a random element from a given collection. These functions are defined in the class Utilities and are global (accessible from each place of model code). You call any function from the list below by typing just its name, without a prefix with the collection name: randomWhere( people, p -> p.income > 10000 );
randomWhere( collection, condition ) - Returns one randomly selected element within the given collection satisfying the specified condition. If the collection is empty, or none elements satisfy the specified condition(s), the function returns null.
Examples: Person wealthyPerson = randomWhere( people, p -> p.income > 10000 );
Person wealthyMan = randomWhere( people, p -> p.income > 10000 && p.sex == MALE );
Here we check two conditions (we use logical AND operator && to tell AnyLogic that both the first AND the second condition should be true). The function returns a randomly selected element that satisfies both conditions.
Person wealthyYoungMan = randomWhere( people, p -> p.income > 10000 && p.sex == MALE && p.age < 21 );
There is another function, enabling the user to use a custom random number generator instead of the default one:
randomWhere( collection, condition, java.util.Random r ) - Similar to randomWhere( population, condition ). Uses the specified random number generator to choose the element.