Thread: Same seed for srand yields different results

  1. #1
    Registered User codegirl's Avatar
    Join Date
    Jun 2003

    Unhappy Same seed for srand yields different results

    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:

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

    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:

    Agent::Agent() {
      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!!
    My programs don't have bugs, they just develop random features.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    srand should only ever be called once, not everytime your constructor is called. For your testing purposes, you can just set the seed to the same value and call srand ONCE with that value. When you are ready to go beyond testing, change the seed value to be based off of the time function call. This probably means you don't need a seed member for the class anymore. You could alter your code so that your constructor will call srand(time(NULL)); if it is the first instance of the class and not during any subsequent instances.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Thanks for the help, hk, I will try that... but I'm still curious as to why I'm not getting the same results. But maybe it's one of those things that you just can't tell without tracing through lots of code!
    My programs don't have bugs, they just develop random features.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Regardless of the multiple srand calls, you should be getting the same results.

    A couple things come to mind as possibilities. Are there any variables that are used prior to being initialized? and are there more calls somewhere to time() or getenv(), or anything else which tries to ask the system for some piece of information?

    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incorrect results from fmod()?
    By karlthetruth in forum C Programming
    Replies: 4
    Last Post: 04-11-2008, 09:12 AM
  2. an altenative PRNG seed (other than time(0))
    By major_small in forum C++ Programming
    Replies: 15
    Last Post: 08-05-2005, 08:05 AM
  3. Results of March Monthly Contest
    By PJYelton in forum Contests Board
    Replies: 23
    Last Post: 04-17-2005, 09:46 AM
  4. 72hour GDC Results
    By jverkoey in forum A Brief History of
    Replies: 3
    Last Post: 07-05-2004, 11:46 PM
  5. Religion
    By gnu-ehacks in forum A Brief History of
    Replies: 239
    Last Post: 01-26-2002, 10:44 AM