random number not fast enough

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manontheedge
    New Member
    • Oct 2006
    • 175

    random number not fast enough

    I've got an issue with the random number generator I'm using in C++. It's randomness is fine, but the problem is that I'm automating something that runs a few thousand times in a couple of seconds, and it's going so fast that the random numbers don't change.

    If I'm manually running the code and taking a second or two in between calls to the function with the random number generator in it, it's fine. It's when it's getting called quickly that it's not changing. I understand that it's going by a clock, I believe the system clock, but it's the only random function I've used that at least gives the appearance of randomness. Is there some way I can get past this, or can someone recommend a random function that doesn't have this problem.

    To be clear what I'm using, I've added a part of my code that uses the random function...

    Code:
            .
            .	
        srand((unsigned)time(NULL)); 
            .
            .		
        m_Value = (rand()%14)+1; 
            .
            .
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    if u want the random number to change everytime u call it then call seed() function befor calling rand and you will be able to achieve what u are looking for.


    Raghu

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Are you perhaps calling srand over and over again?
      The usual practice is for a program to call srand exactly once and then call rand many times.

      Comment

      • meLlamanJefe
        New Member
        • Mar 2008
        • 29

        #4
        If you are calling srand() several times within a second then there is a chance that the seed value is not changing, and because you seed every time you intend to call rand() then then the random number sequence is repeated and will look like it does not change (fast enough). Accoring to the man page for time() the return value is in seconds so for example, if you call time() at 10 times per second the value time() will return will not change during the 10 calls until time advances by one second (does this make sense?). In fact, calling srand() before every call to rand() in the manner you are doing is killing the ability of rand() to produce random number sequences that are distinct from each other every time.

        As mentioned before, you don't typically need to run srand() but once per session, however, if you do want to call srand() every frame and expect that rand() will produce a different sequence of random numbers each frame, then you need to seed the generator with a value that changes each frame (which time() does not when each frame is faster than a second). Not sure if this is desirable for your code but you could call srand with time(NULL) + a frame counter for example (a number that increments every frame producing a different seeding value). Or look at ftime(), it returns a structure where one of the members is the time since the epoch but in milliseconds. This number will change faster, although it still might not be fast enough for your code.

        Good luck!

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          It is hard to generate a truly random sequence of numbers. You don't want to do this yourself -- some really heavy-duty math is involved. Trust the Standard C Library.

          Calling srand with a new seed prior to each rand call means that you are essentially trying to write your own random number generator. Whether the result is truly random depends on the sequence of arguments you pass to srand, the under-the-hood guts of the srand implementation, and the under-the-hood guts of the rand implementation. At best you only know one of those. Unless you can prove otherwise, you have to assume that the result isn't truly random.

          When your program starts, it should call srand once; and then never again. Call rand as often as you need a new random value. If you do this then you're using the Library the way it was meant to be used, giving you the full benefit of the math wizards who implemented rand.

          It might be helpful during debugging if each run of the program yielded the same sequence of random values. If so, then pass a fixed constant value to srand.

          Once you're done debugging you need to decide if your program would do its job better if each run had a unique sequence of random values. If so, then pass a hash value to srand that is likely to be different each time the program runs. System time is a simple hash; but you might feel motivated to do something more complicated that mixes together stuff like date, time, terminal id, and url.

          Comment

          Working...