Security Intermediate

Use the RandomGenerator interface to choose random number algorithms by name without coupling to a specific class.

โœ• Java 8
// Hard-coded to one algorithm
Random rng = new Random();
int value = rng.nextInt(100);

// Or thread-local, but still locked in
int value = ThreadLocalRandom.current()
    .nextInt(100);
โœ“ Java 17+
// Algorithm-agnostic via factory
var rng = RandomGenerator.of("L64X128MixRandom");
int value = rng.nextInt(100);

// Or get a splittable generator
var rng = RandomGeneratorFactory
    .of("L64X128MixRandom").create();
See a problem with this code? Let us know.
๐Ÿ”ง

Algorithm-agnostic

Choose the best RNG algorithm by name without changing code structure.

โšก

Better algorithms

Access to modern LXM generators with superior statistical properties.

๐Ÿ”—

Unified API

One interface covers Random, ThreadLocalRandom, SplittableRandom, and more.

Old Approach
new Random() / ThreadLocalRandom
Modern Approach
RandomGenerator factory
Since JDK
17
Difficulty
Intermediate
RandomGenerator interface
Available

Available since JDK 17 (September 2021, JEP 356).

JDK 17 introduced RandomGenerator as a common interface for all RNG implementations. Instead of hard-coding new Random() or ThreadLocalRandom, you can select algorithms by name via a factory, making it easy to swap between algorithms optimized for different use cases (speed, statistical quality, splittability).

Share ๐• ๐Ÿฆ‹ in โฌก