In a function, I want to generate a list of numbers in range: (This function will be called only once when executing the program.)
void DataSet::finalize(double trainPercent, bool genValidData)
{
srand(time(0));
printf("%d\n", rand());
// indices = {0, 1, 2, 3, 4, ..., m_train.size()-1}
vector<size_t> indices(m_train.size());
for (size_t i = 0; i < indices.size(); i++)
indices[i] = i;
random_shuffle(indices.begin(), indices.end());
// Output
for (size_t i = 0; i < 10; i++)
printf("%ld ", indices[i]);
puts("");
}
The results are like:
850577673
246 239 7 102 41 201 288 23 1 237
After a few seconds:
856981140
246 239 7 102 41 201 288 23 1 237
And more:
857552578
246 239 7 102 41 201 288 23 1 237
Why the function rand()
works properly but `random_shuffle' does not?
srand()
once, at the beginning of your program. Also, see this, and this – Ternarysrand
once. Right? – Lindsyrand()
works andrandom_shuffle()
doesn't seem to have any associations withsrand()
. – Adallardsrand()
call intomain
function, but the results are similar. – Adallardsrand()
. – Whiteliveredrandom_shuffle(begin(indices), end(indices), [](int n) { return rand() % n; });
produces different result, but if I change my random generator to default_random_engine(), then the problems come. So the question becomes that whydefault_random_engine()
not works. – Adallard