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

13
LINES

< > BotCompany Repo | #1003281 // msOneOf - get random entry from MultiSet (honoring element counts of course)

JavaX fragment (include)

1  
static <A> A msOneOf(MultiSet<A> ms) {
2  
  int total = ms.size();
3  
  if (total == 0) ret null;
4  
  int n = random(total);
5  
6  
  for (Map.Entry<A, Integer> entry : ms.map.entrySet()) {
7  
    n -= entry.getValue();
8  
    if (n < 0)
9  
      return entry.getKey();
10  
  }
11  
  
12  
  ret null;
13  
}

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: #1003281
Snippet name: msOneOf - get random entry from MultiSet (honoring element counts of course)
Eternal ID of this version: #1003281/1
Text MD5: cf23db68f94a04a64cfa0109df1346b9
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-06-15 00:07:29
Source code size: 279 bytes / 13 lines
Pitched / IR pitched: No / No
Views / Downloads: 595 / 689
Referenced in: [show references]