-
Notifications
You must be signed in to change notification settings - Fork 0
selectRandomWeighted
ffredyk edited this page Jul 23, 2026
·
1 revision
Random
Unary — selectRandomWeighted <array>.
selectRandomWeighted <array> // [[val1, weight1], [val2, weight2], ...]
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.
- Sums all weights.
- Generates a random number in
[0, totalWeight). - Walks the array, subtracting each weight from the random value until it drops below zero.
- Returns the corresponding value.
_loot = selectRandomWeighted [
["common", 70],
["rare", 25],
["epic", 5]
];
// 70% common, 25% rare, 5% epic_skill = selectRandomWeighted [
["novice", 40],
["regular", 35],
["veteran", 20],
["elite", 5]
];_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 = selectRandomWeighted [
["clear", 60],
["cloudy", 25],
["rain", 10],
["storm", 5]
];Not fully thread-safe for mutation — reads the array. The internal RNG is thread-safe. Array must be accessible from current scheduler.
- selectRandom — uniform random pick
- random — random float
- select — deterministic element access
- Getting Started
- Language Guide
- Type System
- Control Flow
- Functions & Code
- Strings & Text
- Arrays & Collections
- Concurrency
- Promise System
- Thread Safety
- Host API & Embedding
- CLI Tool
- Bytecode Reference
- Multiplayer
- Syntax Sugar
- Optimization Guide
- Benchmarks
- count
- select
- pushBack
- append
- deleteAt
- deleteRange
- resize
- reverse
- sort
- find
- in
- forEach
- freeze
- thaw
- isFrozen
- currentScheduler
- clientOwner
- allSchedulers
- schedulerName
- schedulerExists
- schedulerBudget
- setSchedulerBudget
- fiberCount
- readyFiberCount
- waitingFiberCount
- schedulerLoad
- sendTo