jeudi 2 avril 2020

Is it possible to know how many times rand() function was called?

I am performing experiments that sometimes (cannot predict in advance) generate random numbers using rand() function. The problem is that these experiments take a while (sometimes even a week) and during that time the application or computer can crash. I already implemented a feature to save a log of the current work so I can continue if crash happens, but besides the used seed value I would also like to save how many times the rand() function was already called so when I continue (load from the last log) I don't just reinitialize srand with the same seed value, but to continue from the very next random number it should be generated. One of the solutions I can think of is simply this:

int getRandomValue(int maxValue, int* counter){
    static int count = 0;
    *counter = ++count;
    return rand() % maxValue + 1;
}

And, after loading the last log I would simply call rand() for counter number of times. But, this would require me to change every rand() call with getRandomValue call, and I would like to avoid it if possible, since there are hundreds of places in my code where this should be done. Is there anything more simple than this?




Aucun commentaire:

Enregistrer un commentaire