Guest User

weightedSelectRandom.sqf

a guest
Jan 29th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQF 0.59 KB | None | 0 0
  1. /*
  2.  * Randomly select an item from a weighted array. Array should be in the following format:
  3.  * [[weight0,item0],[weight1,item1]...]. Items with a higher weight are more likely to be
  4.  * chosen; for the array [[1,item0],[2,item1]], there is a 1/3 chance of choosing item0
  5.  * and a 2/3 chance of choosing item1.
  6.  *
  7.  * Example usage: [[1,item0],[1,item1],[2,item2]] call weightedSelectRandom;
  8.  */
  9.  
  10. _sum = 0;
  11. { _sum = _sum + (_x select 0); } forEach _this;
  12.  
  13. _roll = random _sum;
  14.  
  15. _sum = 0;
  16. {
  17.     _sum = _sum + (_x select 0);
  18.     if (_roll < _sum) exitWith {_x select 1};
  19. } forEach _this;
Add Comment
Please, Sign In to add comment