Table of Contents
Testing, Reproducibility and Random Numbers
General
Software needs testing .. and many tests can be automized with unittests - or integration tests.
The program/test should be reproducible to allow (later) debugging/fixing.
Crash early!
Especially when the program or it's results must be reliable, crashing early might be advised!
In this case, additional checks should be enforced and executed. Note, that the C assert()
is removed in non-debug compiled binaries.
Besides executing explicitly programmed checks, there are also other possibilities:
- setup signal handler, e.g. segmentation fault (SIGSEGV)
- setup floating point traps, see Numeric / Math / Linear Algebra
- compile with automatic checks
-
_GLIBCXX_DEBUG
_GLIBCXX_ASSERTIONS
_GLIBCXX_SANITIZE_VECTOR
-
- sanitizers like the prominent AddressSanitizer 'ASan'
- stack-protector
- and others
-
A welcomed side effect of crashing early:
the stack trace and the visible variable contents might reveal the initial cause of the problem
- at least with a much higher probability than after having continued and covered the problem.
Core-Dumps
A core-dump / minidump mechanism might be of interest.
For tracking very rare or specific errors, this mechanism might also get delivered to customers.
Pseudo Random Number Generators (PRNG)
For tests, a huge amount of 'random' numbers can be generated, from one seed-number.
There's no need to have a cryptographically safe PRNG.
Test with defined seed-numbers or simply log/save the used seed number.
Algorithms / Libraries
Properties
- size of the seed or state
- small size is of interest to use one PRNG per thread
- quick initialization from seed
- speed, for producing next number
- period size - until the sequence repeats
Linear congruential generator (LCG)
LCG is perhaps the simplest (and fastest) algorithm with a small seed; see https://en.wikipedia.org/wiki/Linear_congruential_generator
Fast skipping might be of interest; see https://www.nayuki.io/page/fast-skipping-in-a-linear-congruential-generator
Xorshift
Also a very fast PRNG. Here some links
Mersenne Twister
This PRNG got hyped in the C++ community for it's ridiculously huge period size - but has a big seed size and it's initialization isn't the fastest. IMHO, in most applications, the huge period size isn't necessary.
Anyway, here a link to a SIMD-oriented implementation: http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/SFMT/index.html
Other Links
- PCG - comparison of Mersenne Twister, Arc4, ..
- 64 bit version of Mersenne Twister
-
- CppCon 2016: Cheinan Marks “I Just Wanted a Random Integer!
- CppCon 2016: Walter E. Brown “What C++ Programmers Need to Know about Header <random>”
- CppCon 2022: Roth Michaels “Fast, High-Quality Pseudo-Random Numbers for Non-Cryptographers”
- Vectorized (SIMD) generation
- Github