Random number generator for Cilk++

Random number generator for Cilk++


The regular C++ random number generator rand() will introduce race conditions in the software, caught by cilkscreen. The same is true for any naive pseudo random number generator, i.e.

int myrand()
 static int r=15;
 const int M=37;
 const int K=16;
 const int P=100;

 r = (M*r + K) % P;

 return r;

It is clear that the reason is that the generating function needs to remember the last generated number. Placing a mutex around the call to rand() is not a good solution in my case, since I call it a lot while generating random walks for a Monte Carlo simulation.

There are parallel pseudo random number generator algorithms that can be used, when one has complete control over the processes, for instance when using MPI. But how is the situation with Cilk++?

Can anyone recommend a good approach? If it makes things easier, all I need is a random bit, i.e. 0 or 1.

All the best,

Mr Janzon

1 post / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.