Skip navigation links

Package it.unimi.dsi.util

Miscellaneaous utility classes.

See: Description

Package it.unimi.dsi.util Description

Miscellaneaous utility classes.

Pseudorandom number generators

We provide a number of fast, high-quality PRNGs with different features.

xoroshiro128+, xorshift128+ and xorshift1024* provide jump functions which make it possible to generate long non-overlapping sequences, and split functions in the spirit of SplittableRandom.

A table summarizing timings is provided below. Note that we test several different method parameters to highlight the different strategies used to generate numbers in a range, as the rejection-based algorithm used by all generators can be based on integer or long inputs, and the results are quite different. For example, on modern, 64-bit CPUs Java's strategy of applying rejection to 32-bit integers does not pay off (see the timings for nextInt(230 + 1)).

The timings are very different from previous versions, but they should be more reliable, as they are now obtained by means of 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.

Random ThreadLocalRandom SplittableRandom SplitMix64 xoroshiro128+ xorshift128+ xorshift1024*
nextInt() 8.2721.8601.9991.8961.9362.0073.127
nextLong() 17.5491.8862.1232.0541.9942.0123.106
nextDouble() 17.5422.6863.0683.0042.5972.7403.835
nextInt(100000) 8.2853.1633.7693.7133.0703.2644.753
nextInt(229+228) 12.6347.9398.5744.1303.3473.5865.152
nextInt(230) 8.2691.8862.1482.2261.9782.5803.229
nextInt(230 + 1) 22.15215.64516.4833.8373.2443.5285.004
nextInt(230 + 229) 12.5817.9238.5784.0923.3443.5725.138
nextLong(1000000000000) 3.4824.0603.9233.3043.6045.064
nextLong(262 + 1) 16.09016.86416.81113.80514.87818.197

The quality of all generators we provide is very high: for instance, they perform better than WELL1024a or MT19937 (AKA the Mersenne Twister) in the TestU01 BigCrush test suite. More details can be found on the xoroshiro+/xorshift*/xorshift+ generators and the PRNG shootout page.

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). In particular, nextDouble() and nextFloat() use a multiplication-free conversion.

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.

Skip navigation links