I have a very large codebase that uses srand and rand that I need to debug. The random executions make debugging difficult, since bugs occur randomly too. Is there any way to temporary make the library execute deterministically so I can debug the code?
CodePudding user response:
There is no standard way to disable srand
. In some implementations you can redefine it to do nothing and it will work, but that's an ORD violation:
extern "C" void srand(unsigned) noexcept {
// Nothing
}
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main() {
srand((int)time(0));
printf("%d\n", rand());
}
It works in GCC in my testing and prints the same number every time. You probably can use it to debug your code but it's surely not appropriate for production use.
CodePudding user response:
If you can add a file in the include chain, you can put this in that file:
#define rand() (4)
Not very pretty, but could help.