Record of experiments, readings, links, videos and other things that I find on the long road.
Registro de experimentos, lecturas, links, vídeos y otras cosas que voy encontrando en el largo camino.

Wednesday, February 15, 2012

Wrapping a boost random uniform generator in a class.

@irrati0nal and I decided to use Boost.Random as the random generator library in our pet project.
We wanted to wrap it in a class so we could avoid using it directly in our code. This way it would be easier to change to a different random library if we decided to do it.
It was our first experience using the boost libraries. Since we don’t understand templates very well yet, we suffered a bit to get our random generator class working.
We checked the boost documentation example, Bojan Nikolic's examples and some answers in Stack Overflow like this one, but we still didn’t know how to do it. So we decided to hack a solution based on the Stack Overflow answer and Nikolic's examples.
After struggling for a while we finally got to a clumsy solution that compiled and passed the tests:

It also worked but we realized that we didn’t understand very well what was going on, and that the declaration of the generator had become much more complicated.
Since we were not going to be using different random generators or statistical distributions, we decided to keep it simple and stick to the second solution which is easier to understand (for us) and also makes the generator creation easier.
Besides having a lot of fun, we’ve realized how little C++ we know and that we are eager to learn more about templates and the boost library.

PS:
By the way we are using a Mersenne Twister:
"Mersenne Twister: A 623-dimensionally equidistributed uniform pseudo-random number generator", Makoto Matsumoto and Takuji Nishimura, ACM Transactions on Modeling and Computer Simulation: Special Issue on Uniform Random Number Generation, Vol. 8, No. 1, January 1998, pp. 3-30.
I talked about another version of this generator in a previous post.