This repository provides an easy way to compare some common pseudo random number generators (PRNGS) used in C++. It also provides my results running the benchmarks for those wanting a quick answer. To reproduce my results and test for yourself, see section Test for yourself. As of now, the tested PRNG are: Mersenne twister, Linear Congruential Engine, xoshiro / xorshift*, PCG, rand(), Substract with carry and some other more exotic engines.
|
Most of these algorithms should not be used in a context where security is required. |
All these results were compiled over 100 hundred runs (--benchmark_repetitions=100
) using an Intel i5-3570 @ 3.40GHz….. The binary was compiled with clang using -O3 -DNDEBUG, without any architecture optimization. The code is written to avoid any clever trick from the compiler that could bias the results.
PRNG (link) |
Mean time per generation (user) |
Mean generation per second |
Remarks |
1.06ns ± 0.002 ns |
946.757M/s ± 0.719M/s |
||
1.06ns ± 0.002 ns |
946.704M/s ± 0.581M/s |
Fastest for 64 bits, good quality, 400 lines |
|
1.09ns ± 0.010ns |
916.834M/s ± 8.424M/s |
||
0.933ns ± 0.006ns |
1071.970M/s ± 6.220M/s |
Fastest for 32 bits |
|
1.09ns ± 0.010ns |
916.063M/s ± 8.155M/s |
||
1.98ns ± 0.013ns |
504.325M/s ± 3.121M/s |
||
1.31ns ± 0.012ns |
763.500M/s ± 6.629M/s |
||
1.98ns ± 0.008ns |
504.750M/s ± 2.064M/s |
||
1.06ns ± 0.012ns |
940.393M/s ± 10.598M/s |
Not that much faster than pcg-cpp |
|
8.48ns ± 0.112ns |
944.200M/s ± 12.215M/s |
Fastest if your CPU have AVX, mine does not |
|
2.79ns ± 0.023ns |
358.876M/s ± 2.627M/s |
20 lines implementation |
|
15.4ns ± 0.123ns |
65.005M/s ± 0.509M/s |
Slow and poor quality, avoid |
|
3.70ns ± 0.048ns |
270.652M ± 3.418M/s |
Best STL generator for portability |
|
3.40ns ± 0.028ns |
294.261M ± 2.352M/s |
Best STL generator for x86_64 |
|
3.72ns ± 0.038ns |
269.078M ± 2.705M/s |
Worse quality than mt19937 and slower |
|
3.72ns ± 0.033ns |
268.768M ± 2.336M/s |
Worse quality than mt19937 and slower |
|
3.27ns ± 0.022ns |
306.088M ± 2.013M/s |
Poor quality |
|
3.28ns ± 0.038ns |
305.131M ± 3.484/s |
Poor quality |
|
25.8ns ± 0.243ns |
38.809M ± 0.356M/s |
Awfully slow |
|
98.3ns ± 0.926ns |
10.18M ± 0.093M/s |
Awfully slow |
|
14.9ns ± 0.153ns |
66.990M ± 0.660M/s |
Better quality than minstd_rand but as slow as rand |
std::mt19937
and std::mt19937_64
are the best standard C pseudo random number generators in the STL. However, https://www.pcg-random.org/[PCG] is superior in all aspects **for non-cryptographic** uses. https://prng.di.unimi.it/[xoshiro256] is another very good choice for generating 64 bits random numbers, but it does not support multiple streams like PCG.
rand()
need to be avoided at all costs, it is very slow and its quality is bad.
CMake will automatically download all dependencies from other repositories. You just need to have CMake and C++ compiler installed.
-
Clone this repository using git
git clone https://github.com/zeFresk/cpp-random-benchmarks.git
-
Compile using CMake :
cmake . cmake --build .
-
Run and publish your results
./random_benchmarks