GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — RandomGenerator interface | java.evolved

Home / Security / RandomGenerator interface
Security Intermediate

RandomGenerator interface

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

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

Random Number Generation
Code Comparison
✕ 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();
Share 𝕏
See a problem with this code? Let us know.
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
Since JDK
17
Difficulty
Intermediate
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
RandomGenerator ↗ New Random Generator API (JEP 356) ↗
Proof
View proof source ↗
Related patterns
Security Beginner

Strong random generation

Java 8
// Default algorithm — may not be
// the strongest available
SecureRandom random =
    new SecureRandom();
byte[] bytes = new byte[32];
random.nextBytes(bytes);
Java 9+
// Platform's strongest algorithm
SecureRandom random =
    SecureRandom.getInstanceStrong();
byte[] bytes = new byte[32];
random.nextBytes(bytes);
Hover to see modern ➜
JDK 9+ →
Security Advanced

Key Derivation Functions

Java 8
SecretKeyFactory factory =
    SecretKeyFactory.getInstance(
        "PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(
    password, salt, 10000, 256);
SecretKey key =
    factory.generateSecret(spec);
Java 25
var kdf = KDF.getInstance("HKDF-SHA256");
SecretKey key = kdf.deriveKey(
    "AES",
    KDF.HKDFParameterSpec
        .ofExtract()
        .addIKM(inputKey)
        .addSalt(salt)
        .thenExpand(info, 32)
        .build()
);
Hover to see modern ➜
JDK 25+ →
Concurrency Beginner

Virtual threads

Java 8
Thread thread = new Thread(() -> {
    System.out.println("hello");
});
thread.start();
thread.join();
Java 21+
Thread.startVirtualThread(() -> {
    IO.println("hello");
}).join();
Hover to see modern ➜
JDK 21+ →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.