Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Text, Formatting and Regex

The RandomGenerator interface

Reference for java.util.random.RandomGenerator: the Java 17 interface for random numbers, named algorithms, splittable and jumpable generators.

java.util.random.RandomGenerator was added in Java 17 as a common interface for all random number generators. It declares the familiar methods (nextInt, nextLong, nextDouble, nextBoolean, nextBytes, ints, doubles ...) and adds range methods such as nextInt(origin, bound) and nextDouble(origin, bound) that older classes lacked. Random, ThreadLocalRandom, SecureRandom and SplittableRandom all implement it.

The benefit is that code can accept a RandomGenerator and let the caller pick the algorithm. You can also ask for a generator by algorithm name, such as "L64X128MixRandom", through RandomGenerator.of(name) or RandomGeneratorFactory.

Java
RandomGenerator rng = RandomGenerator.of("L64X128MixRandom");
int n = rng.nextInt(1, 7);

Try it

Java Writing code against the interface

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Algorithms by name

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public interface RandomGenerator
static RandomGenerator of(String name)
static RandomGenerator getDefault()
Member Description
nextInt(), nextInt(bound), nextInt(origin, bound) Random int values, optionally bounded.
nextLong(...), nextDouble(...), nextFloat(...) The same for long, double, float.
nextBoolean(), nextBytes(byte[]) Random boolean or bytes.
nextGaussian(), nextExponential() Distributed values.
ints(...), longs(...), doubles(...) Streams of random values.
RandomGenerator.of(name) Generator for a named algorithm (unseeded).
RandomGenerator.getDefault() Generator for the default algorithm (L32X64MixRandom).
RandomGeneratorFactory.of(name) Factory that can create seeded instances and report properties.

Sub-interfaces: SplittableGenerator (create independent child generators for parallel work), JumpableGenerator and LeapableGenerator (skip far ahead in the sequence), StreamableGenerator (streams of generators).

Throws: IllegalArgumentException for an unknown algorithm name or an invalid range.

Usage notes

  • Since Java 17. Before that, Random and its relatives shared no interface.
  • Accept the interface, not Random, in method parameters. Tests can pass a seeded generator and production code a secure one.
  • Algorithm availability depends on the JDK; do not hard-code a name unless you handle the exception. The legacy algorithms Random, SecureRandom and SplittableRandom are always present.
  • Seeds are generator-specific: the same seed gives different sequences for different algorithms.
  • Thread safety is not part of the interface. Most implementations are not thread-safe; use ThreadLocalRandom, one generator per thread, or a splittable generator.
  • Security: only SecureRandom is cryptographically strong. The new algorithms are fast and statistically good but predictable.