First the background info... I've got a program that has several instances of an Agent class, and I wanted each Agent object to have a different seed for srand so that each class produces different random numbers. So in Agent.h I have:

Code:
class Agent {
  ...
private:
  ...
  static int seed;  // Random number generator seed
};
And in Agent.cp, outside of any method, I initialize seed at file scope:

Code:
int Agent::seed = time(NULL);
Finally, within the constructor of the Agent class, I increment seed so that each Agent object has a unique seed:

Code:
Agent::Agent() {
  ++seed; 
  srand (seed); 
  ...
}
The program then uses rand() to generate random numbers, which are then used to generate lots of different results, such as strings and other numbers. There's one big outer loop to my program that loops thru an array of Agent objects, calling various methods for each object. Every so many times through the loop I have it write out the results at that point of the program.

Now here's my problem... I'm trying to reproduce runs of my program to test other parameters, but I want the random numbers that are generated to be the same so that I know that the other parameters are truly what changed the results. So instead of setting seed to time(NULL) like above, I set it to a constant at file scope. When I run the program twice without changing anything, the initial results, which are generated by these random numbers, are the same. But after the program's been executing a while, the later results change. (The program's much too large to just print out all the random numbers, so I just examine the results (strings, etc), that are created from the random numbers.)

Since nothing changed between the two runs and the seed is the same, I would expect the same results. Do I just not understand how srand works? Or does C++ not always execute code in the exact same order everytime? For example, I loop through the Agent objects calling methods for each Agent, and there's lots of loops and function calls in the Agent methods -- maybe C++ is executing the commands in a different order to optimize it? That would explain my varying results.

I hope this makes sense... this program is a few thousand LOC and I didn't want to bore you with all the details, but if you need more info on my program please ask! Thank you!!