mardi 4 juin 2019

Can I generate seed for random numbers as static variable of a function?

In a really schematic way, my aim is to generate good-quality random numbers inside a function. I would like to seed the generator of random numbers with a static variable so I don't have to seed every time that I call the function.

I am generating the random numbers using gsl (https://www.gnu.org/software/gsl/doc/html/rng.html). It is suppose to have better quality than the ones generated with rand() and in a more efficient way that the ones generated with the engine std::mt19937.

gsl_rng* Initialize() { //INITIALIZE
    int rand_seed = 77711;                    //any integer
    srand(time(NULL));
    const gsl_rng_type* gsl_rng_T;
    gsl_rng* r;                        //The random variable
    gsl_rng_env_setup();
    gsl_rng_default_seed = rand_seed;
    gsl_rng_T = gsl_rng_default;
    r = gsl_rng_alloc(gsl_rng_T);
    return r;
}

int random_int(int n) {  //Generate integer random variable in [0,n[
    static gsl_rng* r2 = Initialize(); //Initialize as static
    return gsl_rng_uniform_int(r2, n);
}

void Calculations(/*Variables that have nothing to do with the random numbers*/) {
    //Stuff
    int position == random_int(Info.I);
    //Info.I is an integer member of the class "Info", its value changes //with each call of the function "Calculations".
    //.
    //.
    //.
    return;
}

I have to call the function "Calculations" a lot of times, the values of "position" at each call are highly correlated (not really random). I basically always obtain the same output for "position".

I am quite new in C++, I am used to use FORTRAN and I apologize for the terrible coding!

I related questions, I've seen that people define a class for the seed. Is there any benefit to doing this? Someone recomends a different method or random number generator?

Thank you very much :)




Aucun commentaire:

Enregistrer un commentaire