Package it.unimi.dsi.util
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 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.
Random
| ThreadLocalRandom
| SplittableRandom
| SplitMix64
|
|
|
|
|
|
|
| |
---|---|---|---|---|---|---|---|---|---|---|---|
nextLong() | 14.419 | 1.252 | 1.283 | 1.241 | 1.428 | 1.574 | 1.295 | 1.738 | 1.884 | 1.653 | 1.901 |
nextInt(100000) | 6.715 | 2.045 | 2.499 | 2.543 | 2.336 | 2.594 | 1.202 | 2.607 | 2.954 | 2.367 | 3.119 |
nextDouble() | 14.458 | 1.876 | 2.161 | 2.176 | 1.918 | 2.219 | 1.853 | 2.304 | 2.503 | 2.112 | 2.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
.
-
ClassDescriptionAn abstract implementation of a prefix map.BloomFilter<T>A Bloom filter.Deprecated.A circular char buffer that can be used to implement a sliding window over a text.Compact storage of strings using front-coding compression (also known as compression by prefix omission).An array of approximate sets each represented using a HyperLogLog counter.An immutable implementation of binary tries.A node in the trie.An immutable prefix map mostly stored in external memory.An interval of integers.A class providing static methods and objects that do useful things with intervals.Kahan's summation algorithm encapsulated in an object.A string map based on a function signed using the original list of strings.An interval of longs.A class providing static methods and objects that do useful things with intervals.A
FrontCodedStringList
whose indices are permuted.PrefixMap<S extends CharSequence>A map from prefixes to string intervals (and possibly vice versa).An extension ofPropertiesConfiguration
providing setters for primitive types, a simpler way to save preferences and transparent handling ofEnum
lowercased keys.Provides semi-external random access to a list of γ-encoded integers.Deprecated.There are much better and faster hash functions.A fast, high-quality, non-splittable version of the SplitMix pseudorandom number generator used bySplittableRandom
.A fast, high-quality, non-splittable version of the SplitMix pseudorandom number generator used bySplittableRandom
.StringMap<S extends CharSequence>A map from strings to numbers (and possibly vice versa).A class providing static methods and objects that do useful things with string maps and prefix maps.StringMaps.SynchronizedPrefixMap<S extends CharSequence>StringMaps.SynchronizedStringMap<S extends CharSequence>Ternary interval search trees.Fast pattern matching against a constant string.A fast, all-purpose, rock-solid, small-state pseudorandom number generator.A fast, all-purpose, rock-solid, small-state pseudorandom number generator.A fast, high-quality pseudorandom number generator for floating-point generation.A fast, high-quality pseudorandom number generator for floating-point generation.A fast, all-purpose, rock-solid, small-state pseudorandom number generator.A fast, all-purpose, rock-solid, small-state pseudorandom number generator.A fast, high-quality pseudorandom number generator that combines a long-period instance of George Marsaglia's Xorshift generators (described in “Xorshift RNGs”, Journal of Statistical Software, 8:1−6, 2003) with a multiplication.A fast, high-quality pseudorandom number generator that combines a long-period instance of George Marsaglia's Xorshift generators (described in “Xorshift RNGs”, Journal of Statistical Software, 8:1−6, 2003) with a multiplication.Deprecated.Please useXorShift1024StarPhiRandom
instead.Deprecated.Please useXorShift1024StarPhiRandomGenerator
instead.Deprecated.Please useXoRoShiRo128PlusRandom
instead.Deprecated.Please useXoRoShiRo128PlusRandomGenerator
instead.Deprecated.UseSplitMix64Random
instead.Deprecated.UseSplitMix64RandomGenerator
instead.A fast, all-purpose, rock-solid pseudorandom number generator.A fast, all-purpose, rock-solid pseudorandom number generator.A fast, rock-solid pseudorandom number generator for floating-point generation.A fast, rock-solid pseudorandom number generator for floating-point generation.A fast, all-purpose, rock-solid pseudorandom number generator.A fast, all-purpose, rock-solid pseudorandom number generator.
LongMappedBigList
instead.