dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Resetting the rand() function sequence

  1. #1
    Join Date
    Jun 2004
    Posts
    24

    Resetting the rand() function sequence

    Hi all,
    I have a simulation program which creates some of the working data with the simple rand() function. I know that as long as I give the same seed to the function, it will generate the same sequence of numbers at any given execution of the program. However I need to know how to reset this sequence of numbers.

    Say, if I executed srand(123) and right at the start of the program got 27, 62 and then 110, I want to know how can I reset the rand() function so that it resets that sequence to give me again the 27, 62 and 110, as if the application was closed and executed again. Simply executing srand(123) again won't do the trick.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: Resetting the rand() function sequence

    Looking at the source of rand() [in rand.c] you will see that calling srand with the same seed WILL reset the random number back to the same point. So I am not sure what is going on in your code.....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Jun 2004
    Posts
    24

    Re: Resetting the rand() function sequence

    Oh silly me... I was also using another random function for generating numbers by exponential distribution, and I forgot to reset that as well. That was why I couldn't reproduce the exact same results even after resetting the seed. I was unsure of the effect of using srand again anyway, so I wouldn't have noticed that until I confirmed it, so thanks for the reply!

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: Resetting the rand() function sequence

    Was that the sound of someone smaking their head that I just heard?

    Glad to help
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Aug 2004
    Posts
    52

    Re: Resetting the rand() function sequence

    Well srand(1) should do.

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: Resetting the rand() function sequence

    Only is the random function generator was originally initialized with "srand(1)"

    Remember the random number capabilities of the C/C++ runtime library are well defined. Their behaviour is NOT tha same as functions (with the same or similar names) in other libraries!!!!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Aug 2004
    Posts
    52

    Re: Resetting the rand() function sequence

    The srand function sets the starting point for generating a series of pseudorandom integers. To reinitialize the generator, use 1 as the seed argument. Any other value for seed sets the generator to a random starting point.
    Calling rand before any call to srand generates the same sequence as calling srand with seed passed as 1.

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: Resetting the rand() function sequence

    Daos, you are WRONG. Look at the source code!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Aug 2004
    Posts
    52

    Re: Resetting the rand() function sequence

    Well if he wants the same starting point this should do .The exect same sequance i doubt
    Last edited by Daos; October 3rd, 2004 at 12:49 PM.

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: Resetting the rand() function sequence

    So is the source code [and in this case MSDN and the C++ documentation is not correct (technically) about what the implementation DOES.

    If you have Visual Studio, then you have the source for the run-time library. Posted here [without any permission whatsoever, it the code (with some "compression"]

    Code:
    void __cdecl srand (unsigned int seed)
    {
    #ifdef _MT
      _getptd()->_holdrand = (unsigned long)seed;
    #else  /* _MT */
      holdrand = (long)seed;
    #endif  /* _MT */
    }
    
    int __cdecl rand (void)
    {
    #ifdef _MT
      _ptiddata ptd = _getptd();
      return( ((ptd->_holdrand = ptd->_holdrand * 214013L   + 2531011L) >> 16) & 0x7fff );
    #else  /* _MT */
      return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
    #endif  /* _MT */
    }
    holdrand is a global variable. It is initialized to 0 if nothing else is done.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  11. #11
    Join Date
    Aug 2004
    Posts
    52

    Re: Resetting the rand() function sequence

    Sorry .
    I guess for generating the same sequance of numbers he should use his own function .Like a periodical function cos,sin etc
    that takes the same values .So a random generated number used as an argument to these kind of functions can do the trick if cast to an integer.

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: Resetting the rand() function sequence

    [EDIT] This post is incorrect - see followup

    The above algorithm can be reset to any point in the sequence. It always generates the SAME sequence given a starting, just uses different starting points depending on the seed.

    For example.

    Code:
      srand(123);
      // Get 1000 numbers
      s = rand();
      // Get 1000 more numbers
      srand(s);
      // Get 1000 more numbers
    The second and third set of numbers WILL be identical.
    Last edited by TheCPUWizard; October 3rd, 2004 at 01:44 PM.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  13. #13
    Join Date
    Aug 2004
    Posts
    52

    Re: Nope!

    Well ,not realy i've just tried it on VisualC++6.0 and they are defenetly not IDENTICAL

  14. #14
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: Resetting the rand() function sequence

    Daos,

    you are correct. In my haste I forgot that one needed to preserver the entire 32 bids of holdrand, the code listed doe not properly perform that operation...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  15. #15
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: Resetting the rand() function sequence

    followup:

    If you want to be able to re-seed back to any arbitrary point in the cycle:

    1) Make sure you are using the multi-threaded libraries.

    2) save the value in "_getptd()->_holdrand"

    3) use the above saved value in a call to srand;
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)