Consistent pseudo-random numbers across platforms

2019-01-09 00:42发布

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

标签: c++ c random
6条回答
看我几分像从前
2楼-- · 2019-01-09 01:05

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.

查看更多
Explosion°爆炸
3楼-- · 2019-01-09 01:05

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.

查看更多
相关推荐>>
4楼-- · 2019-01-09 01:22

The easiest way would be to write a random number generator yourself, but using a library that is released for different platforms and is guaranteed to give the same results could also work.

I doubt rand()/srand() are consistent, but I don't know it.

查看更多
不美不萌又怎样
5楼-- · 2019-01-09 01:25

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';
    }
}
查看更多
Rolldiameter
6楼-- · 2019-01-09 01:27

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

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-09 01:28

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.

查看更多
登录 后发表回答