title: How to get current seed from C++ rand()?
body: <p>I generate a few thousand object in my program based on the C++ <em>rand()</em> function. Keeping them in the memory would be exhaustive. Is there a way to copy the CURRENT seed of rand() at any given time? This would give me the opportunity to store ONLY the current seeds and not full objects. (thus I could regenerate those objects, by regenerating the exact same sub-sequences of random numbers)</p>
  
  <p>An exhaustive solution is storing the full sequence of random numbers given by <em>rand()</em> - doesn't worth it.
  Another <em>would be</em> solution is to implement my own class for randomized numbers.</p>
  
  <p>Google gave me no positive clues. There are hundreds of articles teaching the basics of rand and srand, and I couldn't find the specific ones.</p>
  
  <p>Does anyone know other random number generators with implemented seed-stealer?</p>
  
  <hr>
  
  <p>Thank you for your fast answers! There are more possible answers/solutions to this question, so I made a list of your answers here.</p>
  
  <p>SOLUTIONS:</p>
  
  <ol>
  <li><p>The short answer is: there is no standard way to get the seed</p></li>
  <li><p>The closest possible workaround is to save the INITIAL seed in the beginning, and count how many times you call the rand() function. I marked this as solution because it works on the current <em>std::rand()</em> function of <em>every compiler</em> (and this was the main question about). I've benchmarked my 2.0 GHz CPU, and found that I can call&amp;count <em>rand()</em> 1,000,000,000 times in 35 seconds. This might sound good, but I have 80,000 calls to generate one object. This restricts the number of generations to 50,000 because the size of unsigned long. Anyway, here is my code:</p>
  
  <pre><code>class rand2
  {
     unsigned long n;
  
     public:
  
     rand2 () : n(0) {}
  
     unsigned long rnd()
     {
        n++;
        return rand();
     }
     // get number of rand() calls inside this object
     unsigned long getno ()
     {
        return n;
     }
     // fast forward to a saved position called rec
     void fast_forward (unsigned long rec)
     {
        while (n &lt; rec) rnd();
     }
  };
  </code></pre></li>
  <li><p>Another way is to implement your own Pseudo-random number generator, like the one Matteo Italia suggested. This is the fastest, and possibly the BEST solution. You're not restricted to 4,294,967,295 <em>rand()</em> calls, and don't need to use other libraries either. It's worth mentioning that different compilers have different generators. I've compared Matteo's <em>LCG</em> with <em>rand()</em> in Mingw/GCC 3.4.2 and G++ 4.3.2. All 3 of them were different (with <em>seed = 0</em>).</p></li>
  <li><p>Use generators from C++11 or other libraries as Cubbi, Jerry Coffin and Mike Seymour suggested. This is the best idea, if you're already working with them.
  Link for C++11 generators: <a href="http://en.cppreference.com/w/cpp/numeric/random" rel="noreferrer">http://en.cppreference.com/w/cpp/numeric/random</a> 
  (there are some algorithm descriptions here too)</p></li>
  </ol>