Logo

Programming-Idioms

History of Idiom 11 > diff from v73 to v74

Edit summary for version 74 by programming-idioms.org:
[Java] Emphasize comments

Version 73

2019-05-02, 09:08:31

Version 74

2019-05-05, 16:11:15

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;
Imports
import java.util.concurrent.ThreadLocalRandom;
Code
x.get(ThreadLocalRandom.current().nextInt(0, x.size()))
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
Comments bubble
Using ThreadLocalRandom prevents unnecessary allocation of a new Random, and is more efficient in a multi-threaded application