mercredi 16 mars 2016

create a customized c++11 random sequence

I have already had a naive random generator written in C

inline double rnd(void){
    static double ISEED = 38467.;
    ISEED = fmod(1027.*ISEED, 1048576.);
    return (ISEED / 1048576.);
}

now I want to rewrite it in C++ 11 way, I did some research, but it does not work, please help. following is the code I got so far:

#include <iostream>

#include <random>
#include <chrono>


using namespace std;

inline double rnd(void) {
    static double ISEED = 38467.;
    ISEED = fmod(1027.*ISEED, 1048576.);
    return (ISEED / 1048576.);
}

int main(int argc, char* argv[]) {
    linear_congruential_engine<unsigned long long, 1027, 0, 1048576> generator(38467);
    uniform_real_distribution<double> ud(0.0, 1.0);
    cout << ud(generator) << " - " << rnd() << endl
         << ud(generator) << " - " << rnd() << endl
         << ud(generator) << " - " << rnd() << endl
         << ud(generator) << " - " << rnd() << endl
         << ud(generator) << " - " << rnd() << endl
         << ud(generator) << " - " << rnd() << endl
         << ud(generator) << " - " << rnd() << endl;
    return 0;
}

what I expected is when code running, numbers in same line should be same.

thanks in advance.




Aucun commentaire:

Enregistrer un commentaire