vendredi 22 février 2019

c++ random set seed failed

I am trying to set seed to the c++ std::default_random_engine:

#include<random>
#include<time.h>
#include<iostream>

using namespace std;


void print_rand();


int main() {

for (int i{0}; i < 20; ++i) {
    print_rand();
}
return 0;
}

void print_rand() {
    default_random_engine e;
    e.seed(time(0));

    cout << e() << endl;
}  

It seems that the printed numbers are same, how could I set the seed to generate the random number according to the time?

Aucun commentaire:

Enregistrer un commentaire