Fluky - random number generator library
I was always curious about Random Number Generator (RNG) implementation. The magic how from few numbers inside it generates long sequence “randomly” distributed numbers. I did few experiments on my ZX Spectrum with it limited performance. And this article I would like to share the fluky library.
All RNG implemented in fluky are pseudo random generators dependant just from internal state and initial seed, no external entropy. So “random” in text below mean pseudo random.
In fluky was implemented few RNG based on different principles of getting random number.
- Linear congruential generator (LCG)
- Permuted Congruential Generator (PCG)
- Middle-square RNG
and many more (ref).
Currently, fluky provides few LCG (quick and simple in implementation), PCG and Square example. The RNG could be compared statistically, by size of internal state and by performance of calculation next random number.
The simplest statistical visualization - just draw a million of generated numbers on canvas.
The worst as you can imagine appear ZX Spectrum LCG. It operates just 216(65536 values) and visualization has not more than 128x128 pixels.
The most performant and with perfect quality (by dieharder test and benchmarks) is Small Prng. The cycle length is expected to be about 2126 results. On my laptop where single number generated in 2.327 ns/op it would take 1.158×1039 years (ref).
A flagship RNG in current experiment I would consider Pcg64
It has good performance and statistical background. Also mentioned as a good alternative of Mersenne Twister RNG.
Also, it’s important to mention the Small Prng and Pcg64 are passing 113 tests and have just 1 weak test with no failings.
How to test Random Number Generator
For testing random number generators could be used different cli applications to check statistics of generated random numbers on relatively large data set. For example dieharder requires approximately 229 GB of data for 114 different tests (more info). Other projects like NIST used 15 different statistical tests.
If you need more info about testing RNG please check related StackOverflow or StackExchange questions.