AnyLogic provides functions to select a random agent from a given population. 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 population name: randomWhere( people, p -> p.income > 10000 );
Function | Description |
---|---|
Agent randomWhere(population, condition) |
Returns one randomly selected agent within the given population satisfying the specified condition. If the population is empty, or no agents 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 agent that satisfies both conditions. Person wealthyYoungMan = randomWhere( people, p -> p.income > 10000 && p.sex == MALE && p.age < 21 ); |
Agent randomWhere(population, condition, java.util.Random r) | Similar to randomWhere(population, condition). Uses the specified custom random number generator to choose the element. |
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 );
Function | Description |
---|---|
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 ); |
randomWhere(collection, condition, java.util.Random r) | Similar to randomWhere(population, condition). Uses the specified random number generator to choose the element. |
AnyLogic provides functions to select a random element from a given array. 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 array name: randomWhere( people, p -> p.income > 10000 );
Function | Description |
---|---|
T randomFrom(T[] array) | Returns a randomly chosen element of the given array (or null if the array is empty). This result of this function is an equivalent of calling: array[uniform_discr( array.length - 1 ) ] |
T randomFrom(T[] array, java.util.Random r) | Similar to randomFrom(T[] array). Uses the specified custom random number generator to choose the element. |
T randomWhere(T[] array, condition) | Returns a randomly chosen element within the given array satisfying the specified condition. If the array is empty or no elements satisfy the specified condition(s), the function returns null. |
T randomWhere(T[] array, condition, java.util.Random r) | Similar to randomWhere(T[] array, condition). Uses the specified custom random number generator to choose the element. |
AnyLogic provides functions to select a random element from the given enumeration. 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.
Function | Description |
---|---|
<T extends java.lang.Enum<T>> T randomFrom(java.lang.Class<T> enumeration) | Returns the randomly chosen constant from the given enumeration class. Throws NullPointerException if the given class is null or not an enumeration class. |
<T extends java.lang.Enum<T>> T randomFrom(java.lang.Class<T> enumeration, java.util.Random r) | Returns the randomly chosen constant from the given enumeration class. Uses the specified custom random number generator r to choose the constant. Throws NullPointerException if the given class is null or not an enumeration class. |
For example, you have the option list named Gender. To randomly select one option from this option list, use one of the following functions:
- randomFrom(Gender.class);
- Gender.random(this);
To select a random standard color, one of possible 140 standard colors, use the global function java.awt.Color randomColor().
AnyLogic provides functions to create a randomly chosen object from a given list of objects. These functions are defined in the Utilities class and are global (accessible everywhere in the model). You call any function from the list below by typing just its name, without a prefix.
Function | Description |
---|---|
<T> T randomlyCreate(java.lang.Class<? extends T>... classes) |
Creates a randomly chosen object using one of the given constructors of object classes. Returns randomly chosen and created object, or null if there are no classes provided. Example usage (in the New rail car field of the TrainSource block of the Rail Library): randomlyCreate(OpenCar.class, BoxCar.class, HopperCar.class); classes — object classes |
<T> T randomlyCreate(java.util.function.Supplier<? extends T>... constructors) |
Creates a randomly chosen object using one of the given constructors. Returns randomly chosen and created object, or null if there are no classes provided. Example usage (in the New rail car field of the TrainSource block of the Rail Library): randomlyCreate(OpenCar::new, BoxCar::new, HopperCar::new); constructors — constructors or other functions / lambda expressions which return object instances |
<T> T randomlyCreate(java.util.Random r, java.lang.Class<? extends T>... classes) |
Creates a randomly chosen object using one of the given constructors of object classes. The choice is made using the specified custom random number generator r. Returns randomly chosen and created object, or null if there are no classes provided. Example usage (in the New rail car field of the TrainSource block of the Rail Library): randomlyCreate(OpenCar.class, BoxCar.class, HopperCar.class); r — the custom random number generator classes — object classes |
<T> T randomlyCreate(java.util.Random r, java.util.function.Supplier<? extends T>... constructors) |
Creates a randomly chosen object using one of the given constructors. The choice is made using the specified custom random number generator r. Returns randomly chosen and created object, or null if there are no classes provided. Example usage (in the New rail car field of the TrainSource block of the Rail Library): randomlyCreate(OpenCar::new, BoxCar::new, HopperCar::new); r — the custom random number generator constructors — constructors or other functions / lambda expressions which return object instances |
-
How can we improve this article?
-