RandomGenerator interface
Use the RandomGenerator interface to choose random number algorithms by name without coupling to a specific class.
Porównanie kodu
✕ 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();
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
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.
Stare podejście
new Random() / ThreadLocalRandom
Nowoczesne podejście
RandomGenerator factory
Od JDK
17
Poziom trudności
Średniozaawansowany
Wsparcie JDK
RandomGenerator interface
Dostępne
Available since JDK 17 (September 2021, JEP 356).
Jak to działa
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).
Powiązana dokumentacja