joikd 10 Posted January 8, 2013 (edited) If I have two arrays: _myItemsArray = [gun, knife, grenade, rocket]; _myItemsChanceArray = [.2, .7, .03, .005]; how can I build a new array based on the % chance for each item using individual rolls for each item (yes, that last one is a 5 in 1000 chance)? I want a new array that possibly contains any/all/none of those items. Something like this if both gun & grenade made their rolls, but knife and rocket did not: _newArray = [gun, grenade]; By the way, the first two arrays might be very large (close to 100 items). Any help would be greatly appreciated. Edited January 8, 2013 by joikd Share this post Link to post Share on other sites
Muzzleflash 108 Posted January 8, 2013 You would go through each element in the array and then "roll a dice" for each element. 1 equals 100 %, (1 in 1 chance). If we generate a random between 0 and 1, then that number will be less than 0.005 in 5 out of 1000 times.: PickByChance = { private ["_elements", "_chances", "_result"]; _elements = _this select 0; _chances = _this select 1; _result = []; { if (random 1 <= (_chances select _forEachIndex)) then { _result set [count _result, _x]; }; } forEach _elements; _result }; //To get the random items: _randomItems = [_myItemsArray, _myItemsChanceArray] call PickByChance; Note that _forEachIndex is only available in OA (or CO) not in vanilla Arma 2 (unless they patched it in). Share this post Link to post Share on other sites
joikd 10 Posted January 8, 2013 Thanks, Muzzleflash! How many decimal points can "random 1" go out to? Can it be used to roll 1 in 1,000,000? Share this post Link to post Share on other sites
Muzzleflash 108 Posted January 8, 2013 Hmm that depends on how these, "floating-point", numbers are handled by the engine (wikipedia says 6-9 decimals - but again it depends on the engine and the random generator). 1 out of 1,000,000 is probably gonna be okay, although 1 out of 1,000,000,000 might not work properly. However, don't really see the point? You would have to run the script 1,000,000 times on average before it happens. Unless you run the command many times during a mission, it will probably never be noticed. Share this post Link to post Share on other sites
joikd 10 Posted January 8, 2013 I don't plan on using 1 in 1,000,000--just wanted to know the limit just in case. Thanks again! Share this post Link to post Share on other sites