Skip to content

selectRandom

ffredyk edited this page Jul 23, 2026 · 1 revision

selectRandom

Category

Random

Arity

UnaryselectRandom <array>.

selectRandom <array>

Description

Picks and returns a random element from the array. Each element has equal probability. Returns nil if the array is empty.

How It Works

Generates a random index from 0 to count-1 using the thread-safe RNG, then returns array[index]. All elements have uniform probability.

Usage

Basic random pick

_color = selectRandom ["red", "green", "blue"];
_sound = selectRandom _soundArray;

Random spawn position

_spawnPos = selectRandom _spawnPoints;

Random AI behavior

_behavior = selectRandom ["patrol", "guard", "idle", "search"];
_unit setBehavior _behavior;

Empty array guard

_result = selectRandom _arr;
if (isNil "_result") then {
    systemChat "No items available";
};

Dice table simulation

_lootTable = ["common", "common", "common", "common", "rare", "epic"];
_loot = selectRandom _lootTable;  // 4/6 common, 1/6 rare, 1/6 epic

Thread Safety

ReadOnly on the array — only reads the array content. The internal RNG is thread-safe. Array must be accessible from current scheduler (frozen OK, mutable owned OK).

See Also

SQ# Wiki

Home

Engine Docs

Migration

Commands

Value Constructors

Arithmetic

Comparison

Logic

Array

String

Math

Random

Type & Introspection

HashMap

Code Execution

Concurrency

Scheduler

Thread Safety

Error

Output

Time

Multiplayer

Compiler

Clone this wiki locally