DEV Community

Discussion on: Share a code snippet in any programming language and describe what's going on

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️
private["_min", "_max", "_mid", "_target", "_kit"];
_target = random(_sum);
_min = 0; _max = count _array -1;

while { _min != _max-1 } do {
   _mid = floor (_min + (_max - _min) / 2);
   if ( (_array select _mid select 0) <= _target )
      then { _min = _mid }
      else { _max = _mid };
};
Enter fullscreen mode Exit fullscreen mode

Does a binary search through an array and finds the first element with a value greater than a random target at index 0.

This is a part of a script that chooses a random set of items to put in a supply box where each item set can have a weight assigned to it based on how valuable the items are (higher weight means more common)

Collapse
 
pandademic profile image
Pandademic

what language is that?

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

SQF :D