Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consistent pseudo-random numbers across platforms

Tags:

c++

c

random

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

like image 882
toastie Avatar asked May 28 '09 18:05

toastie


People also ask

Are pseudo random numbers uniformly distributed?

Methods of sampling a non-uniform distribution are typically based on the availability of a pseudo-random number generator producing numbers X that are uniformly distributed.

Can pseudo random numbers be predicted?

The outcome of the research confirms the possibility that machine learning algorithms can be trained to predict certain PRNGs. Even when trained with a small amount of data, there is evidence that machine learning algorithms can be used to predict the values created by pseudorandom number generators.

What is a commonly used algorithm to generate pseudo random numbers?

Widely used PRNG algorithms : Lagged Fibonacci generators, linear feedback shift registers, Blum Blum Shub.


4 Answers

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

like image 91
rlbond Avatar answered Sep 21 '22 08:09

rlbond


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.

like image 25
las3rjock Avatar answered Sep 22 '22 08:09

las3rjock


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';
    }
}
like image 40
Shafik Yaghmour Avatar answered Sep 18 '22 08:09

Shafik Yaghmour


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.

like image 27
Craig McQueen Avatar answered Sep 22 '22 08:09

Craig McQueen