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

6
LINES

< > BotCompany Repo | #1013017 // randomFromIterator - can return null if n is not correct

JavaX fragment (include)

static <A> A randomFromIterator(int n, Iterator<A> it) {
  n = random(n);
  while (n-- > 0)
    if (it.hasNext()) it.next(); else null;
  ret it.hasNext() ? it.next() : 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: #1013017
Snippet name: randomFromIterator - can return null if n is not correct
Eternal ID of this version: #1013017/1
Text MD5: 87d2c48b197f13f84f4bb51f4be82fca
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-21 03:37:17
Source code size: 181 bytes / 6 lines
Pitched / IR pitched: No / No
Views / Downloads: 388 / 401
Referenced in: [show references]