Results 1 to 5 of 5

Thread: Req: Random selection script

  1. #1

    Question Req: Random selection script

    Hi,

    I need help with a task I can't accomplish myself, because I'm lacking any major skills in scripting.

    The script I need has to do the following:

    Let's say I've an array with 20 items in it. The script should randomly select 8 of these items and put them in another array. None of the items should be picked twice, so the script has to check which items were already picked and don't select them again.

    I don't have any idea how to do this, so if anyone is willing to give me a hand I would be very grateful!

  2. #2
    Warrant Officer Demonized's Avatar
    Join Date
    Nov 16 2010
    Location
    Back from afk 2013
    Posts
    2,614
    PHP Code:
    _allItems = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
    _selectedItems = [];
    _howManyToSelect 8;
    while {
    _howManyToSelect != 0} do {
       
    _add _allItems select (floor(random(count _allItems)));
       if (!(
    _add in _selectedItems)) then {
          
    _selectedItems selectedItems + [_add];
          
    _howManyToSelect  howManyToSelect  1;
       };
    }; 
    My scripts:
    Spoiler:

    what to do when posting any kind of code dammit!!

    Any new mission editor or scripter in Arma2 should have read Mr Murrays Editing Guide Deluxe at least once, it still applies for A2 even though it was made for Armed Assault.

  3. #3
    Code:
    _allItems = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; 
    _selectedItems = []; 
    _howManyToSelect = 8; 
    for "_i" from 0 to (_howManyToSelect - 1) do {
      _add = _allItems select (floor(random(count _allItems))); 
      _selectedItems set [_i,_add];
      _allItems = _allItems - [_add];
    };

  4. #4
    If you wish to permit duplicates then this should work correctly. Assuming you have no use of 'objNull'.

    Code:
    _allItems = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; 
    _selectedItems = []; 
    _howManyToSelect = 8;
    
    _copy = +_allItems;
    for "_i" from 0 to (_howManyToSelect - 1) do {
      _add = _copy select (floor(random(count _copy))); 
      _selectedItems set [_i,_add];
      _copy set [_i, objNull];
      _copy = _copy - [objNull];
    };

  5. #5
    Dear all,

    thanks you very much for your help! Your scripts worked very well and now my mission is working as planned. Looking at your work also gave me a better understanding how some commands work, so if there's another problem I may be able to solve it myself.

    Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •