What algorithms are used in modern and good-quality random number generators?
4 Answers
In R, the default setting for random number generation are:
- For U(0,1), use the Mersenne-Twister algorithm
- For Guassian numbers use the numerical inversion of the standard normal distribution function.
You can easily check this, viz.
> RNGkind()
[1] "Mersenne-Twister" "Inversion"
It is possible to change the default generator to other PRNGs, such as Super-Duper,Wichmann-Hill, Marsaglia-Multicarry or even a user-supplied PRNG. See the ?RNGkind for further details. I have never needed to change the default PRNG.
The C GSL library also uses the Mersenne-Twister by default.
-
$\begingroup$ Are you sure about your second point, generating normal random variables by inverting the CDF? The inverse of the normal CDF is a fairly expensive function to evaluate. I imagine Box-Muller's method would be faster. Faster still would be Marsaglia's ziggurat method for generating normals. $\endgroup$ Commented Aug 25, 2010 at 14:53
-
$\begingroup$ I also find this suspicious. Marsaglia's Ziggurat is the default in Matlab, and I can't imagine Matlab being better than R in the field of random number generation. $\endgroup$ Commented Aug 25, 2010 at 16:21
-
$\begingroup$ @John Indeed, the polar method is available in R, see the setRNG package. $\endgroup$– chlCommented Aug 25, 2010 at 21:28
-
$\begingroup$ @John it turns out the Box-Muller method is more sensitive in the tails to the discreteness of the 32-bit random number stream that goes into it. R switched to inversion several years ago because the quality is better even though it's slower. $\endgroup$ Commented Jul 3 at 4:07
-
$\begingroup$ e.g., Python (j.mp/9TVyhv), Perl (j.mp/by85El), Octave (j.mp/aVQ5Xz), Clojure (j.mp/dkj9Z9, not the default one), Haskell (j.mp/aWK7kK), lua (j.mp/bSD2vO), or even SQL (j.mp/aOPJMW, for an overview) :-) $\endgroup$– chlCommented Aug 25, 2010 at 21:41
The Xorshift PNG designed by George Marsaglia. Its period (2^128-1) is much shorter than the Mersenne-Twister but the algorithm is very simple to implement and lends itself to parallelization. Performs well on many-core architectures such as DSP chips and Nvidia's Tesla.
-
$\begingroup$ Would this be good for implementing on GPUs? Link to details, references? $\endgroup$– DarenWCommented Aug 2, 2010 at 0:27
-
3$\begingroup$ Thomas, Howes, Luk - 2009 - A comparison of CPUs, GPUs, FPGAs, and massively parallel processor arrays for random number generation. doi.acm.org/10.1145/1508128.1508139. Discussion + benchmarks of a set of PNGs executing on CPU, GPU, FPGA and Massively Parallel Processor Arrays. $\endgroup$– brotchieCommented Aug 9, 2010 at 1:12
-
1$\begingroup$ Maybe also L'Ecuyer's RNG with multiple streams (j.mp/bzJSlm)? $\endgroup$– chlCommented Aug 25, 2010 at 21:08
At http://prng.di.unimi.it/ you can find a shootout of several random number generators tested using TestU01, the modern test suite for pseudorandom number generators that replaced diehard and dieharder. You can pick and choose.