Logo

Programming-Idioms

History of Idiom 11 > diff from v72 to v73

Edit summary for version 73 by Yorizzz:
New Java implementation by user [Yorizzz]

Version 72

2018-11-21, 12:56:45

Version 73

2019-05-02, 09:08:31

Idiom #11 Pick a random element from a list

List x must be non-empty.

Illustration

Idiom #11 Pick a random element from a list

List x must be non-empty.

Illustration
Extra Keywords
choose
Extra Keywords
choose
Imports
import java.util.concurrent.ThreadLocalRandom;
Code
x.get(ThreadLocalRandom.current().nextInt(0, x.size()))
Comments bubble
using ThreadLocalRandom prevents unnecessary allocation of a new Random, and is more efficient in a multi-threaded application