Restarting the random number generator to get the same random values

Hi all,

I am trying to restart the random number generator from a previously stored seed, but I cannot get it to work using GetSeed and SetSeed. This code does not work:

void RestartRandom()
{
  unsigned int StartSeed = 123456;
  int Loops = 5;

  gRandom->SetSeed(StartSeed);
  
  cout<<"First batch with original seed: "<<endl;
  for (unsigned int i = 0; i < Loops; ++i) {
    cout<<gRandom->GetSeed()<<": "<<gRandom->Rndm()<<endl;
  }
  unsigned int Seed = gRandom->GetSeed();
  
  cout<<"Second batch, a few more: "<<endl;
  for (unsigned int i = 0; i < Loops; ++i) {
    cout<<gRandom->GetSeed()<<": "<<gRandom->Rndm()<<endl;
  }
 
  cout<<"Third batch - set the seed to the one before the second batch in order to reproduce the second batch: "<<endl;
  gRandom->SetSeed(Seed);
  for (unsigned int i = 0; i < Loops; ++i) {
    cout<<gRandom->GetSeed()<<": "<<gRandom->Rndm()<<endl;
  }
}

Are there any other possibilities to restart the random number generator? I do not see anything in the source code…

Thanks,
Andreas


Thanks a lot. The solution then seems to be to copy the TRandom3 object:

void RestartRandom()
{
  unsigned int StartSeed = 123456;
  unsigned int Loops = 5;

  gRandom->SetSeed(StartSeed);
  
  cout<<"First batch with original seed: "<<endl;
  for (unsigned int i = 0; i < Loops; ++i) {
    cout<<gRandom->Rndm()<<endl;
  }
  
  // Make a copy of the object to restart it
  TRandom3* R = new TRandom3(*((TRandom3*) gRandom));
  
  cout<<"Second batch, a few more: "<<endl;
  for (unsigned int i = 0; i < Loops; ++i) {
    cout<<gRandom->Rndm()<<endl;
  }
 
  cout<<"Third batch - Use the copy of the old random number generator: "<<endl;
  delete gRandom;
  gRandom = R;
  for (unsigned int i = 0; i < Loops; ++i) {
    cout<<gRandom->Rndm()<<endl;
  }
}

In addition, I strongly suggest that you add to the ROOT documentation that the seed value obtained via GetSeed cannot be used to set the seed in TRandom3.