jeudi 9 février 2017

Creating a uniform random number generator

I am trying to create a random number generator that will return an array of length n of random numbers between a and 'b'. For some reason I get the same value each time and I was certain I have the correct seed in the srand function.

Here is my code:

double fRand(int n,double fMin, double fMax) {
    srand (time(NULL));
    double array[n];
    double f = (double)rand() / RAND_MAX;
    for(int i = 0; i < n; i++){
        array[i] = fMin + f * (fMax - fMin);
    }
    for(int i = 0; i < n; i++){
        cout << array[i] << endl;
    }
}

I am new to C++ so the syntax or data structure may not look the best.




Aucun commentaire:

Enregistrer un commentaire