dimanche 18 novembre 2018

Random numbers seeded by time don't change

To generate random numbers between 0 and 1, I have written the following but of code:

 double random_0_to_1(){
    srand (time(NULL));
    random();
    return (double)random() / (double)RAND_MAX;
}

int main(){

    for(int i = 0 ; i < 10 ; i++){
        double temp = random_0_to_1();
        printf("%f\n", temp);
    }
    return 0;
}

The result that is generated however is always the same, no matter how many times I call it. The same numbers are always generated. I have tried a lot of different ways, but can't seem to find anything that works. Is there a way to generate random numbers that are different every time I call random_0_to_1?




Aucun commentaire:

Enregistrer un commentaire