mardi 25 janvier 2022

Why does my random function generate the same number every time I call it in a loop?

I have my random function generating a pseudo-random number in the range:

double Utils::randomNumber(int min, int max)
{
    assert(min < max);
    srand(time(nullptr));
    return (max - min) * ((double)rand() / RAND_MAX) + min;
}

However, when I call it in a loop, I always get the very same number, although I seed the sequence:

for (int i = 0; i < m_inputs; ++i)
{
    m_weights.push_back(Utils::randomNumber(0, 1));
}

Output:

Weights: [0.787561, 0.787561, 0.787561, 0.787561, 0.787561, 0.787561, 0.787561, 0.787561, 0.787561, 0.787561, 0.787561, 0.787561]



Aucun commentaire:

Enregistrer un commentaire