32

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).

2
  • 2
    see 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: stackoverflow.com/questions/34903356/… Commented Jan 23, 2016 at 11:46
  • If you choose to use linear congruential generators, this wikipedia page 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. Commented Aug 21, 2019 at 17:08

5 Answers 5

19

Something like a Mersenne Twister (from Boost.Random) is deterministic.

Sign up to request clarification or add additional context in comments.

2 Comments

If you'd rather avoid Boost, you can use the original implementation of the Mersenne Twister, 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.
see 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: stackoverflow.com/questions/34903356/…
8

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.

Comments

6

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'; } } 

2 Comments

The 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++.
see 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: stackoverflow.com/questions/34903356/…
1

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.

Comments

0

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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.