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.

A table summarizing timings is provided below. The timings were measured on an Intel® Core™ i7-8700B CPU @3.20GHz using JMH microbenchmarks. The JMH timings were decreased by 1ns, as using the low-level perfasm profiler the JMH overhead was estimated at ≈1ns per call.

Timings in nanoseconds for a few generators
Random ThreadLocalRandom SplittableRandom SplitMix64 xoroshiro128++ xoroshiro128** xoroshiro128+ xoshiro256++ xoshiro256** xoshiro256+ xorshift1024*φ
nextLong() 14.4191.2521.2831.2411.4281.5741.2951.7381.8841.6531.901
nextInt(100000) 6.7152.0452.4992.5432.3362.5941.2022.6072.9542.3673.119
nextDouble() 14.4581.8762.1612.1761.9182.2191.8532.3042.5032.1122.755

Note that generators that are extremely fast in C, such as xoshiro256+, do not perform particularly well in Java, most likely because of the cost of accessing variables, which rises as the size of the state space grows. Indeed, smaller-state generators are faster. Moreover, generators based on the ++ scrambler are slightly faster than those based on the ** scrambler, contrarily to what happens in C.

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.