Random number generators discussion

Random number generators | www.agner.org

thread How to get the seed from a running random process? - Thuy Nguyen - 2010-07-19
last reply How to get the seed from a running random process? - Agner - 2010-07-19
How to get the seed from a running random process?
Author:  Date: 2010-07-19 00:40
Hi Agner,

Thank you for your great software. I am trying to use your random generator for my research. What I am doing is simulating time-consuming Monte Carlo simulations, maybe several days or even a week. Sometimes my lab is in outage during my simulation so that I need to rerun my program again. I want to save all my program variables in a file at a point in order to redo my program from that point if anything occur. So I wonder whether there is anyway I can get the seed from your random generators during simulations?

Thanks again,

Thuy Nguyen

How to get the seed from a running random process?
Author: Agner Date: 2010-07-19 06:59
To save the state of the random number generator, you need to save all the data. Use a C++ version (or C++ interface to an assembly language version) and save all data defined in the class to a binary file. These data are private or protected so you need to make a member function to save the data and another member function to restore the data. For example:

class CRandomMersenne {
void SaveToFile(FILE * f) {
   fwrite(mt, sizeof(uint32_t), MERS_N, f);
   fwrite(&mti, sizeof(int), 1, f);
   fwrite(&LastInterval, sizeof(uint32_t), 1, f);
   fwrite(&RLimit, sizeof(uint32_t), 1, f);
void RecoverFromFile(FILE * f) {
   fread(mt, sizeof(uint32_t), MERS_N, f);
   fread(&mti, sizeof(int), 1, f);
   fread(&LastInterval, sizeof(uint32_t), 1, f);
   fread(&RLimit, sizeof(uint32_t), 1, f);

Remember to open the file in binary mode and to close it again.