# selectRandomWeighted ## Category Random ## Arity **Unary** — `selectRandomWeighted `. ``` selectRandomWeighted // [[val1, weight1], [val2, weight2], ...] ``` ## Description Picks and returns a random element based on weighted probabilities. The right operand is an array of `[value, weight]` pairs. Higher weight = more likely to be chosen. Returns `nil` if the array is empty. ## How It Works 1. Sums all weights. 2. Generates a random number in `[0, totalWeight)`. 3. Walks the array, subtracting each weight from the random value until it drops below zero. 4. Returns the corresponding value. ## Usage ### Basic weighted pick ```sqf _loot = selectRandomWeighted [ ["common", 70], ["rare", 25], ["epic", 5] ]; // 70% common, 25% rare, 5% epic ``` ### AI skill randomization ```sqf _skill = selectRandomWeighted [ ["novice", 40], ["regular", 35], ["veteran", 20], ["elite", 5] ]; ``` ### Loot drop table ```sqf _drop = selectRandomWeighted [ [nil, 50], // 50% nothing ["gold", 30], // 30% gold ["potion", 15], // 15% potion ["sword", 5] // 5% sword ]; if (!isNil "_drop") then { giveItem(_drop); }; ``` ### Weather selection ```sqf _weather = selectRandomWeighted [ ["clear", 60], ["cloudy", 25], ["rain", 10], ["storm", 5] ]; ``` ## Thread Safety **Not fully thread-safe for mutation** — reads the array. The internal RNG is thread-safe. Array must be accessible from current scheduler. ## See Also - [selectRandom](selectRandom.md) — uniform random pick - [random](random.md) — random float - [select](select.md) — deterministic element access