I generate a few thousand object in my program based on the C++ rand() function. Keeping them in the memory would be exhaustive. Is there a way to copy the CURRENT
rand()
does not offer any way to extract or duplicate the seed. The best you can do is store the initial value of the seed when you set it with srand()
, and then reconstruct the whole sequence from that.
The Posix function rand_r()
gives you control of the seed.
The C++11 library includes a random number library based on sequence-generating "engines"; these engines are copyable, and allow their state to be extracted and restored with <<
and >>
operators, so that you can capture the state of a sequence at any time. Very similar libraries are available in TR1 and Boost, if you can't use C++11 yet.