I am looking for a way to generate pseudo random number sequences that will yield identical sequence results for a given seed across any platform. I am assuming that rand()
/ srand()
is not going to be consistent (I could easily be wrong about this assumption).

- 41,871
- 30
- 130
- 181

- 1,934
- 3
- 22
- 35
-
2see my question and answer for C++11 random generators. The C++11 mt19937 delivers consistent results across all platforms, but the standard distributions do not, so I created my own distributions: http://stackoverflow.com/questions/34903356/c11-random-number-distributions-are-not-consistent-across-platforms-what-al – Arno Duvenhage Jan 23 '16 at 11:46
-
If you choose to use linear congruential generators, [this wikipedia page](https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use) is useful. In particular you could use the C implementation of `rand` suggested in the ISO/IEC 9899. In my tests it gave identical results when `unsigned long` is using 32 (the minimum) or 64 bits. – Gabriel Devillers Aug 21 '19 at 17:08
5 Answers
Something like a Mersenne Twister (from Boost.Random) is deterministic.

- 65,341
- 56
- 178
- 228
-
1If you'd rather avoid Boost, you can use the original implementation of the [Mersenne Twister](http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html), which was written in straight C. In recent years, the MT group has added some additional ports that can make use of SIMD, OpenCL, and CUDA. – sfstewman Mar 19 '13 at 18:19
-
3see my question and answer for C++11 random generators. The C++11 mt19937 delivers consistent results across all platforms, but the standard distributions do not, so I created my own distributions: http://stackoverflow.com/questions/34903356/c11-random-number-distributions-are-not-consistent-across-platforms-what-al – Arno Duvenhage Jan 23 '16 at 11:43
Knuth has released into the public domain C (and FORTRAN) source code for the pseudo-random number generator described in section 3.6 of The Art of Computer Programming.

- 8,612
- 1
- 31
- 33
I realize this is an old thread but now with C++11 there are a whole bunch of new options available. Here is a distilled example from the page which defaults to using the Mersenne Twister
engine and Normal
distribution:
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
int main()
{
std::random_device rd;
//
// Engines
//
std::mt19937 e2(rd());
//std::knuth_b e2(rd());
//std::default_random_engine e2(rd()) ;
//
// Distribtuions
//
std::normal_distribution<> dist(2, 2);
//std::student_t_distribution<> dist(5);
//std::poisson_distribution<> dist(2);
//std::extreme_value_distribution<> dist(0,2);
std::map<int, int> hist;
for (int n = 0; n < 10000; ++n) {
++hist[std::round(dist(e2))];
}
for (auto p : hist) {
std::cout << std::fixed << std::setprecision(1) << std::setw(2)
<< p.first << ' ' << std::string(p.second/200, '*') << '\n';
}
}

- 154,301
- 39
- 440
- 740
-
8The distributions specified by the C++11 standard are **not** required to yield reproducible results given the same seed, even when using the same code. Let alone using different libraries, like libc++ vs libstdc++. – OmnipotentEntity Oct 27 '14 at 18:21
-
2see my question and answer for C++11 random generators. The C++11 mt19937 delivers consistent results across all platforms, but the standard distributions do not, so I created my own distributions: http://stackoverflow.com/questions/34903356/c11-random-number-distributions-are-not-consistent-across-platforms-what-al – Arno Duvenhage Jan 23 '16 at 11:45
I've been working on a simplerandom
library for this. It is supposed to be cross-platform, and I also aim to target multiple languages. Currently it supports C and Python (same numbers generated in both languages). I plan to implement the same generators in C++ soon, following the Boost and C++11 random API.

- 41,871
- 30
- 130
- 181
A quickly googled reference says:
Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.
But the question remains. I assume the above spec only applies to RNGs within the same process. It most likely doesn't specify anything about cross-platform or cross-compiler things. Your best bet is probably to find a library that is available for all desired platforms. Then you should be reasonably safe that if seeded with the same value they return the same sequence of numbers.

- 5,989
- 4
- 39
- 56
-
I believe this refers to he behavior of any particular implementation of `rand()`, and does not provide any cross-compiler or cross-platform assurance. Moreover, with built-in PRNGs you risk all kinds of unwanted properties. Use a library with a PRNG known to be appropriate for your needs. – dmckee --- ex-moderator kitten May 28 '09 at 19:00