Update: Version 0.9.9
It became clear that things didn't really work properly with Boost 1.42, so I've refactored the package to try to help you kludge Boost Random 1.47 into place if necessary; this seems to work; see with cpprandpar.m file for build help.
===============
Update: Version 0.9.5 now has wired in many flexible distributions, so you can use the same RNG to generate pseudorandom samples from many distributions. What I have for now is: uniform_01, uniform_int, uniform_smallint, uniform_real, poisson, normal, triangle. I also have piecewise_linear, which allows you to specify your own continuous probability density. Should be simple to get more in.
New syntax:
d = createdist('normal_distribution', [0 1.5]);
A = cpprand(10000, 100, uint32(5489), d);
hist(A(:), 100);
This was a little kludgy to wire together, so I'm keeping both v0.8.0 and v0.9.5 in the zip for now in case people have trouble building or running the new version. I think it should be possible to flexibly wire in different underlying generators as well, but this will be messy and I don't have a real need at the moment for anything other than Mersenne Twister. Will probably try at least CMWC though, in case it is much faster.
===============
This package wraps standard C++ Random Number Generators (RNGs). Although it currently relies on the Boost Random library, this is included in the new C++11 standard, so as C++11 compilers become available we should be able to drop the Boost dependency. Therefore I named the package CppRand.
This is a little harder to get set up than some mex libraries, so some experience with compiling C++ is recommended.
Currently I have only implemented wrappers for the Mersenne Twister implementation. This is the same as the default RNG for recent versions of Matlab. Importantly, the Boost 1.42 implementation is actually slower than the latest Matlab implementations, but the Boost 1.47 implementation is faster.
In addition to the simple wrapper, I have included a multithreaded version for parallel RNG. This relies on the Theron Actors library, which in turn relies on either Windows threads or Boost Threads. Again, thread support is coming in C++11, at which point external dependencies can be removed. See http://absurdlycertain.blogspot.com/2011/09/preamble-what-follows-is-guide.html for a detailed guide on getting Theron set up.
Please see the included M-files for more documentation.
Benchmarks on a 4-core Linux machine:
Boost 1.47:
>> tic; for i = 1:10, rand(1000000,10); clear ans; end; toc; clear i
Elapsed time is 1.151257 seconds.
>> tic; for i = 1:10, cpprand(1000000,10,uint32(5489)); clear ans; end; toc; clear i
Elapsed time is 0.729212 seconds.
>> tic; for i = 1:10, cpprandpar(1000000,10,uint32(5489:5492)); clear ans; end; toc; clear i
Elapsed time is 0.335498 seconds.
Boost 1.42:
>> tic; for i = 1:10, cpprandpar(1000000,10,uint32(5489:5492)); clear ans; end; toc; clear i
Elapsed time is 1.405573 seconds.
SLOWER than Matab rand even with 4 threads! |