samedi 3 décembre 2022

Why do I get nearly the same number out of the rand() function in the first iteration?

in my C99-programme I want to use pseudo-random numbers between 0 an 1. Unfortunatly, my programme always generates a first number that is almost identical. It just steps up ever so slightly every time I rerun my programme.

This is the relevant part of my programme:

srand(time(NULL));  
for(int i = 0; i < 10; i++){
    float a = (float)rand()/RAND_MAX;
    printf("%f\n",a);

And here are the results of two Iterations with a time difference of below 10 seconds:

0.717103
0.357464
0.903628
0.271930
0.327478
0.917489
0.231215
0.026307
0.135259
0.290941

0.717221
0.330531
0.237708
0.151682
0.318986
0.201876
0.936884
0.209277
0.324705
0.311334

I tried to generate completely different numbers no matter how many I computed before, but the first is alway close to the same.




Aucun commentaire:

Enregistrer un commentaire