โ— Shell
clean mode source โ†—

RandomGenerator interface

Code Comparison

// 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);
// 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();

Why the modern way wins

๐Ÿ”ง

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

JDK Support

RandomGenerator interface

Available

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

How it works

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).

Related Documentation

Proof