Package it.unimi.dsi.util


package it.unimi.dsi.util
Miscellaneous utility classes

Pseudorandom number generators

Warning: before release 2.6.3, the split() method of all generators would not alter the state of the caller, and it would return instances initialized in the same way if called multiple times. This was a major mistake in the implementation and it has been fixed, but as a consequence the output of the caller after a call to split() is now different, and the result of split() is initialized in a different way.

We provide a number of fast, high-quality PRNGs with different features. You can get detailed information about the generators at our PRNG page, together with a reasoned guide to the choice of the generator that's right for you.

Note that starting with Java 17 xoroshiro128++ and xoshiro256++ are part of the package java.util.random.

A table summarizing timings is provided below. The timings were measured on a 12th Gen Intel® Core™ i7-12700KF @3.60GHz using JMH microbenchmarks on the GraalVM virtual machine for Java 19 (release 22.3.0).

Timings in nanoseconds for a few generators
Random ThreadLocalRandom SplittableRandom SplitMix64 xoroshiro128++ xoroshiro128** xoroshiro128+ xoshiro256++ xoshiro256** xoshiro256+ xorshift1024*φ
nextLong() 14.522 0.699 0.536 0.518 0.817 0.937 0.732 0.911 0.852 0.760 0.776
nextDouble() 14.513 1.813 1.609 1.608 1.607 1.609 1.608 1.607 1.610 1.608 1.609
nextInt(100000) 7.652 1.329 1.368 1.194 1.322 1.428 1.216 1.751 1.763 1.990 1.286
nextInt(230+1) 16.291 10.722 9.639 1.260 1.348 1.471 1.240 1.851 1.862 1.890 1.351

For each generator, we provide a version that extends Random, overriding (as usual) the next(int) method. Nonetheless, since the generators are all inherently 64-bit also nextInt(), nextFloat(), nextLong(), nextDouble(), nextBoolean() and nextBytes(byte[]) have been overridden for speed (preserving, of course, Random's semantics).

If you do not need an instance of Random, or if you need a RandomGenerator to use with Commons Math, there is for each generator a corresponding RandomGenerator implementation, which indeed we suggest to use in general if you do not need a generator implementing Random.