Home > Community Toolbox Reference > Array utilities > Retrieving random items
Returns a random element from the array or its subsection. If the array/subsection is empty, undefined is returned.
Argument | Type | Usage | Description |
---|---|---|---|
array | Array |
Required | The array to get the random element from. |
offset | Real |
Default: 0 |
The starting index of the subsection (for a negative offset, it will count from array end). |
length | Real |
Default: undefined |
The length of the subsection (for a negative length, it will count backwards from the offset position). |
Any
The following code picks a random word from the nouns dictionary:
randomize();
var _known_nouns = ["Apple", "Banana", "Cherry", "Date", "Elderberry"];
var _random_noun = array_get_random(_known_nouns);
show_debug_message(_random_noun);
- 23.4.0 - Created a function to get a random value from an array.