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)

static <A> A msOneOf(MultiSet<A> ms) {
  int total = ms.size();
  if (total == 0) ret null;
  int n = random(total);

  for (Map.Entry<A, Integer> entry : ms.map.entrySet()) {
    n -= entry.getValue();
    if (n < 0)
      return entry.getKey();
  }
  
  ret null;
}

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: 594 / 688
Referenced in: [show references]