vendredi 28 février 2020

Random is always the same with seed C++

I got always the same random numbers. What's wrong?

int main() {
    srand(time(NULL));

bank_t* bank = allocBank(numberOfAccounts);


thread threads[numberThreads];
Work work;

for (int i = 0; i < numberThreads; i++) {
    threads[i] = thread{work};
}
for (int i = 0; i < numberThreads; i++) {
    threads[i].join();
}

return 0;
}

I tryed rand() with srand(time(NULL)) in main:

class Work {                                                //Funktor
public:
void operator() () const {

transferMoney(rand()% numberOfAccounts, rand()% numberOfAccounts);
}
};

I also tryed default_random_engine generator which should be Thread Safe?

default_random_engine generator;
uniform_int_distribution<int> distribution(0,numberOfAccounts);


class Work {                                                //Funktor
public:
void operator() () const {

transferMoney(distribution(generator), distribution(generator));
}
};



Aucun commentaire:

Enregistrer un commentaire