different rand() results on Windows and Linux

Boost has a wide range of RNGs, presumably with reproducible behaviour across platforms.


You won't get the same results due to the different implementation of the functions on either platform.


  • Write your own (not recommended).
  • Use a library. e.g. Boost
  • This function

If you’re happy with the standard rand implementation and only require reproduceability, you can easily write your own linear congruential generator (adapting the C interface, probably not a good choice! – rather use a class instead):

namespace myown {

static int state;

void srand(int seed) {
    state = seed;
}

int rand() {
    int const a = 1103515245;
    int const c = 12345;
    state = a * state + c;
    return (state >> 16) & 0x7FFF;
}

}

This uses constants (ANSI C: Watcom) from the Wikipedia article.

That said, I’d rather go with a read-made implementation from Boost, as proposed by others.

Tags:

C++

Random