1

I'm working on a small distributed computing project. The server can't handle the calculations without crashing Apache in the process, so I'm giving these calculations to the users.

The basic idea is that I run the calculations 3 times, and the results need to be exactly the same. However, the application will need to run on Mac, Linux and Windows. Basically this means that I can't use rand().

I'm probably looking for a replacement generator which accepts a seed value (provided by the server) and gives the same results on every platform.

My question is: is that indeed what I am looking for and do you have a good recommendation, or should I be doing this in a different way?

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105

3 Answers3

7

Take a look at the Boost.Random facilities. They give you a broad choice of algorithms that are guaranteed to give the same results, always.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • +1 for being faster than me. ;-) But yes, I believe Boost.Random will give portable results across different platforms. – C. K. Young Jun 10 '11 at 16:12
2

Write your own. It's surprisingly straightforward.

"Numerical Recipes in C - The Art of Scientific Computing" has several algorithms, and I can present you just one (“Minimal” random number generator of Park and Miller.) :

#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
#define MASK 123459876



float ran0(long *idum)
{
 long k;
 float ans;
 *idum ^= MASK;
 k=(*idum)/IQ;
 *idum=IA*(*idum-k*IQ)-IR*k;
 if (*idum < 0) *idum += IM;
  ans=AM*(*idum);
 *idum ^= MASK;
 return ans;
}

Returns a uniform random deviate between 0.0 and 1.0. Set or reset idum to any integer value (except the unlikely value MASK) to initialize the sequence; idum must not be altered between calls for successive deviates in a sequence.

EDIT

Since you need good performances for a RNG, you can use a quick and dirty algorithm. Here is one :

unsigned long idum; // initialize the value to something
// get a RND
idum = 1664525L*idum + 1013904223L;
BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • 1
    Its both surprisingly straightforward, and surprisingly easy to get wrong. – Dennis Zickefoose Jun 10 '11 at 16:35
  • Looks good and I assume that the performance of this code is pretty good as well. However I'll just be honest and say that I have absolutely no idea what happens in the code above and until I do I'm probably sticking with Boost. Of course, if I get performance issues I'll make sure to consider this code :) +1 for showing me how "easy" it is. – Tom van der Woerdt Jun 10 '11 at 16:41
  • @Tom Off course there are other algorithms in the book, but I doubt you will have performance problems with the boost library – BЈовић Jun 10 '11 at 16:44
  • @VJo I think that more than a third of all calculations done is related to random numbers. The actual bottleneck of the application will, of course, be the speed of the RAM, but I want to optimize wherever I can. For now, however, I'm going for simplicity. – Tom van der Woerdt Jun 10 '11 at 16:47
  • @Tom In that case, you can a quick and dirty RNG – BЈовић Jun 10 '11 at 16:52
0

The boost random number library is very good - there's an example of its use here Boost random number generator. As far as I'm aware, its implementations produce the same results cross platform, but I've never tested this.

Community
  • 1
  • 1