Java 17 PRNG API

Java 17 come up with new API for Pseudo-Random Number Generators. There are several resons behind introducing new PRNG API. Which provides uniform ways to use existing and new PRNG algorithms.

New interface is introduced due to following reasons,

  • ThreadLocalRandom extends Random class and overrides pretty similar set of methods which can be avoided by using interface.
  • To replace usage of SplittableRandom PRGN objects with Random we have to change every variable even though the classes having some similar behaviors.
  • Code duplication in Random, SplittableRandom and ThreadLocalRandom
  • Make it easy to introduce new PRGN implementation without much code changes.
  • Provide support for PRGNs which are jumpable or leapable. (i.e. Xoroshiro128+)

What does new Random generator API provides?

Following diagram can explain hierarchy of random generators,

As we can see RandomGenerator is the parent interface extended by StreamableGenerator interface which enables streams of object to generate statistically independent objects.

Two child interfaces SplittableGenerator and JumpableGenerator extends StreamableGenerator interface which are introduced as part of Java 17.

Also, old Random class is now extending the RandomGenerator, which is done to bring older random generation into one umbrella and preserve it. As a result child class SecureRandom now automatically supports RandomGenerator. Thus we won’t need to reimplement SecureReandom class or it’s associated implementations. 

SplittableGenerator

Allows to create new SplittableGenerator from existing one which will generate statistically independent results. After split, high probability is that both objects will together generate values with same statistical properties like it is generated by single object. Provides two major split() and splits() methods to return split off and effectively unlimited stream of splits from existing generator.

Worth to note that the SplittableRandom (which is an implementation of SplittableGenerator) instances are not thread-safe.

Java 17 also has implementation of LXM family of PRNG algorithms which includes following classes,

  • L32X64MixRandom
  • L32X64StarStarRandom
  • L64X128MixRandom
  • L64X128StarStarRandom
  • L64X256MixRandom
  • L64X1024MixRandom
  • L128X128MixRandom
  • L128X256MixRandom
  • L128X1024MixRandom

JumpableGenerator

Allows to generate random values and jump to a point in state cycle.

Provides jump() method, which jump forward to fixed distance (typically 264). Also, jumps() method returns effectively unlimited stream of object post jumping forward. This are major methods, but it also provides other methods as well like jumpDistance() which returns the distance by which the jump() method will jump forward, copyAndJump() which copies existing generator, jumps it forward and returns the copy etc. Unlike split() which returns the split off object, jump() is void which just jumps.

LeapableGenerator interface extends JumpableGenerator. Which allow to jump ahead a large number of draws.

Following implementations of JumpableGenerator are provided in Java 17,

  • Xoshiro256PlusPlus
  • Xoroshiro128PlusPlus

Sample program to get implemented random generators in Java 17, Output:
Provided RandomGenerators :
L32X64MixRandom
L128X128MixRandom
L64X128MixRandom
SecureRandom
L128X1024MixRandom
L64X128StarStarRandom
Xoshiro256PlusPlus
L64X256MixRandom
Random
Xoroshiro128PlusPlus
L128X256MixRandom
SplittableRandom
L64X1024MixRandom
---------
Create RandomGenerator :Xoshiro256PlusPlus
-1083384208

Reference :

No comments:

Post a Comment