dimanche 7 juin 2015

Linear congruential generator in C++

I wrote a simple program (tried to implement the Linear congruential generator actually), but I'm not quite sure it works like it should.

I wanted to generate 250 number from [0,1] using my generator. However, it seems that instead of random numbers, I get equal values ..

How to improve it / what I did wrong?

Here's the code:

#include <iostream>
#include <cmath>

static const double A = 0.001342;
static const double C = 0.00025194;
static const double RAND_MAX = 1.0;

double rand()
{
    static double prev = 0;
    prev = A * prev + fmod(C, RAND_MAX);
    return prev;
}

int main(int argc, char **argv)
{
    for(int i=0; i<6; i++)
    std::cout << rand() << "\n";
    return 0;
}

And the output:

0.00025194
0.000252278
0.000252279
0.000252279
0.000252279
0.000252279




Aucun commentaire:

Enregistrer un commentaire