mardi 19 octobre 2021

probleme with random float [duplicate]

I have a problem with the generation of random float I dont understand why each time i run my program the first value I get is always the same ...


#include <iostream>
#include <random>
#include <time.h>
using namespace std;

int main()
{
    srand(time(NULL));
    for(int i = 0 ; i < 15 ; i++)
    {
        cout << 10 + static_cast <float>(rand()) /(static_cast <float>(RAND_MAX/(100-10))) << endl;
    }
  return 0;
}

two exemple of run :

39.0208
62.6987
20.3247
75.0493
44.7273
26.6675
83.5688
69.2312
43.2182
21.7714
70.1896
22.1844
23.8597
40.2026
80.1221

the second one :

39.2156
23.9169
66.1948
65.7013
65.1429
5.91688
14.174
64.5792
8.28312
80.0442
61.3532
24.8156
69.7662
7.34545
44.0987

I mean it is not exactly the same but you got the idea i can run it 100 times i always get "39.something "

I dont understand why it does that and how I can solve this ...

thank you for your help




Aucun commentaire:

Enregistrer un commentaire