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. As a general review:

Both XorShift128PlusRandom and XorShift1024StarRandom provide jump functions which make it possible to generate long non-overlapping sequences.

A table summarizing timings is provided below. Note that we test several different method parameters to show the large gap between full 64-bit generators and ThreadLocalRandom.

ThreadLocalRandom SplittableRandom SplitMix64RandomGenerator XorShift128PlusRandom XorShift1024StarRandom
nextInt() 1.721.261.171.472.18
nextLong() 1.661.251.191.352.07
nextDouble() 2.072.071.541.552.21
nextInt(1000000) 2.852.662.232.833.31
nextInt(2^29+2^28) 7.447.042.693.143.60
nextInt(2^30) 1.801.381.602.392.83
nextInt(2^30+1) 14.9813.522.402.963.33
nextInt(2^30+2^29) 7.366.862.523.013.52
nextLong(1000000000000) 2.682.622.742.713.70
nextLong(2^62+1) 14.9514.0914.5412.9015.35

Unfortunately, we have no way to control the optimizations performed by the JVM. In C, for example, we use gcc's -fno-move-loop-invariants and -fno-unroll-loops options. These options are essential to get a sensible result: without them, the optimizer can move outside the testing loop constant loads (e.g., multiplicative constants). SplittableRandom and SplitMix64RandomGenerator are particularly advantaged in this respect, as they contain three large constants whose load can be moved outside the testing loop. The best advice is always that of measuring the speed of your application with different generators.

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. In particular, SplitMix64Random, XorShift128PlusRandom and XorShift1024StarRandom pass BigCrush. More details can be found on the 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