{"name":"selectRandomWeighted","title":"selectRandomWeighted","type":"command","url":"https://community.bistudio.com/wiki/selectRandomWeighted","revid":377677,"updated":"2026-01-02T09:02:40Z","groups":["Arrays"],"status":"ok","games":{"arma3":"1.76"},"since":"arma3 1.76","arma3_since":"1.76","description":"Returns a random element from the given array according to assigned weights. Engine solution to BIS_fnc_selectRandomWeighted. A few notes:\n\n- Negative weights are not supported\n- If an item's weight is 0, the item is ignored\n- The weights don't have to total to 1\n- If numbers of items and weights do not match, the shortest array is used\n- A single array Syntax is slightly faster than Alt Syntax","syntaxes":[{"syntax":"selectRandomWeighted valuesAndWeights","params":[{"name":"valuesAndWeights","type":"Array with [valueN, weightN]","description":"Array with [valueN, weightN]\n\n- valueN: Anything - weighted value\n- weightN: Number - values relative weight","type_expr":"Array","text":"valuesAndWeights: Array with [valueN, weightN]\n\n- valueN: Anything - weighted value\n- weightN: Number - values relative weight"}],"returns":{"type":"Anything","type_expr":"Anything","description":"random weighted value or Nothing if array is empty or all weights are 0"},"form":"unary","right":{"text":"valuesAndWeights"}},{"syntax":"values selectRandomWeighted weights","params":[{"name":"values","type":"Array of Anything","type_expr":"Array<Anything>"},{"name":"weights","type":"Array of Numbers","type_expr":"Array<Number>"}],"returns":{"type":"Anything","type_expr":"Anything","description":"random weighted value or Nothing if any of arrays is empty or all weights are 0"},"form":"binary","left":{"text":"values"},"right":{"text":"weights"}}],"examples":[{"code":["private _randomWeightedElement = selectRandomWeighted [1,0.1,2,0.2,3,1.3,4,2.4,5,7.5];"]},{"code":["private _randomWeightedElement = [1,2,3,4,5] selectRandomWeighted [0.1,0.2,1.3,2.4,7.5];"]}],"see_also":["random","select","selectRandom","selectMin","selectMax"],"notes":[{"user":"Killzone_Kid","date":"2017-06-26T19:17:00Z","text":"A 100000 runs of the following code:\n\n```sqf\n_values = [0,1,2,3,4,5,6,7,8,9];\n_weights = [0.109,0.65,0.01,1.01,1.24,0,1.59,0.09,1.15,0.55];\n_result = _values selectRandomWeighted _weights;\n```\n\nGave this result:\n\n```\n0 - 0\n0.01 - 144\n0.09 - 1409\n0.109 - 1633\n0.55 - 8673\n0.65 - 10127\n1.01 - 15668\n1.15 - 18059\n1.24 - 19518\n1.59 - 24769\n```"}]}