jeudi 24 septembre 2015

Generating two ( or multiple) uniformly distributed random numbers c++

I would like to generate several uniformly distributed random numbers in the same pass. So far I have a "standard" function for generating a random number

double generateRandomNumber()
{
    srand((unsigned)time(NULL));
    double r=((double)rand()/(double)RAND_MAX);
    return r;
}

how ever when in main I call it like that:

# include <iostream>
# include <string>
# include <cmath>
# include <ctime>

int main()
{

    // generate random number
    double rr1=generateRandomNumber();
    double rr2=generateRandomNumber();
    cout << rr1 << endl;
    cout << rr2 << endl;
    return 0;
}

I get that both numbers are the same ( I guess its the time limitations of seconds), anyways, this is something I would like to generelize to multiple random numbers.

Can anyone suggest a better way? maybe using a different method or library?




Aucun commentaire:

Enregistrer un commentaire