Victus Spiritus


Uniform and Normal Random Number Generator Shootout

19 Jun 2011

If you have a definitive need for speed like myself, then standard tactics for random number generation simply won't do. In the project I'm working on I don't include or link to Boost (Mersenne Twister) because it's a small delivery, so I went hunting for a fast, reliable, and well packaged random number generator. I remembered reading John D. Cook's blog post sharing precisely what I was looking for. The fastest approach I could generate quickly is a cached version of a large number of normal calls accessed by a uniform call in the loop.

First the source (here's the repo where I keep my c++ utils):

And of course the results:

Random number generator speed tests

Thanks to John D. Cook

to build on *nix:

g++ -O3 -o testRNG testRNG.cpp SimpleRNG.cpp -lm

Quality test:

Mark-Essels-iMac:proj messel$ ./testRNG 1000000
rng get uniform time 0.012 mean 0.499806 stddev 0.2887
rand() get uniform time 0.013 mean 0.50003 stddev 0.288526
generated normal 0.135 mean 0.000684646 stddev 1.00001
rng Normal time 0.085 mean 0.000477835 stddev 0.999851
rng stored normal accessed with uniform 0.006 mean 0.000477835 stddev 0.999851

Speed test: note, with optimizations John's uniform was faster so I used it for the cached normal approach

Mark-Essels-iMac:proj messel$ ./testRNG 100000000
rng get uniform time 1.030
rand() get uniform time 1.178
generated normal 14.099
rng Normal time 8.393
rng stored normal accessed with uniform 0.486