Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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;

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top