Search code examples
c++randomunmanagedsrand

Find out what a random number generator was seeded with in C++


I've got an unmanaged c++ console application in which I'm using srand() and rand(). I don't need this to solve a particular problem, but was curious: is the original seed passed to srand() stored somewhere in memory that I can query? Is there any way to figure out what the seed was?


Solution

  • The seed is not required to be stored, only the last random number returned is.

    Here's the example from the manpage:

           static unsigned long next = 1;
    
           /* RAND_MAX assumed to be 32767 */
           int myrand(void) {
               next = next * 1103515245 + 12345;
               return((unsigned)(next/65536) % 32768);
           }
    
           void mysrand(unsigned seed) {
               next = seed;
           }