Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

12
LINES

< > BotCompany Repo | #1008665 // selectRandom2 - select n random elements from list (in random order, no duplicates), return rest list also

JavaX fragment (include)

1  
// returns selected, rest
2  
static <A> Pair<L<A>> selectRandom2(L<A> list, int n) {
3  
  if (l(list) < n) null;
4  
  new L<A> l;
5  
  list = cloneList(list);
6  
  repeat n {
7  
    int i = rand(l(list));
8  
    l.add(list.get(i));
9  
    list.remove(i);
10  
  }
11  
  ret pair(l, list);
12  
}

Author comment

Began life as a copy of #1002448

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1008665
Snippet name: selectRandom2 - select n random elements from list (in random order, no duplicates), return rest list also
Eternal ID of this version: #1008665/4
Text MD5: 0eebaab529fa17dd815074c1b3695728
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-05-27 14:03:47
Source code size: 268 bytes / 12 lines
Pitched / IR pitched: No / No
Views / Downloads: 432 / 472
Version history: 3 change(s)
Referenced in: [show references]