vendredi 22 février 2019

C++ random numbers not random even with seed

I am trying to generate random numbers. I seeded the number generator but the following code produces numbers increasing by ~10 or so every time I run the program (if I don't wait long enough). So if I run it once I might get, say, 251. Then I run it immediately afterwards and I get 260 or so. If I wait a bit longer I might get 300, etc. What is happening?

#include <iostream>
#include <ctime>

int main()
{
    srand(time(NULL));
    int r = rand()%1000;

    std::cout << r << std::endl;
    return 0;
}




Aucun commentaire:

Enregistrer un commentaire