Security intermediate

RandomGenerator interface

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

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