Get a specific "random" for TRandom

Dear experts,

I run over 100 TRandom [1], but now I’m interested in the 50th random number. I do not want to rerun from 0, so I wonder how I can specify to the TRandom to run only the 50th random number?

Regards

[1]
TRandom *random = new TRandom3();
for(int i=0; i<100; i++){

double stepG1{random->Rndm()}; // option to get the 20th configuration

}

Hi,

There is no function to jump in the sequence and generate only the 50th number.
Since it is very fast to generate the random numbers, you can easily reran from 0 to 50.

Lorenzo

Hi,
if you really want to start at - say 50 - you could remember the seed value(50) once:

UInt_t seed50;
TRandom * r = new TRandom();
for (Int_t i=0; i < 50; i++) {
   Double_t x = r->Rndm();
}
seed50 = r->GetSeed();
Double_t y = r->Rndm();
r->SetSeed(seed50);
y = r->Rndm();

For using SetSeed please consult also:

https://root.cern.ch/doc/master/classTRandom.html

Otto

Hi,

Get/SetSeed will work only with TRandom, who as a state consisting of a single 32 bit word. It will not work with the other generators who have a much larger state. And TRandom is also a generator who should not be used, given its bad random properties.

The alternative possibility is to save the TRandom object. If you save in a file then the full state will be stored, and after reading it back, it will continue generate the numbers form the state it was before.

Lorenzo

Dear experts,

-I need to start at a specific random number because for each random I’m doing complicated fits which take time, and I may need to debug for random 99…
-actually I was looking for something like the seed method… Thank you.

Regards

Hi,

The best solution is then is that you save the TRandom3 object for that particular event in a file using the Write() function

Lorenzo

Dear Lorenzo,

“save the TRandom3 object for that particular event in a file using the Write() function” then I guess that I should re-open that file, find the object etc… this seems “complicated” for what I want to do (just start from a specific random) . Do you have a small macro to do that?

Regards

Hello,

Would it be possible to do the same with ROOT::Math::RandomMixMax? By now the interpreter says "no member named ‘Write’ " nor SaveAs.

Reading the reference, I tried to define a std::vector< StateInt_t > statusmixmax, in order to call GetState, but I get "use of undeclared identifier ‘StateInt_t’ ".
Thank you!

Hi,

You should be possible to save the MixMax generator if you use the TRandom wrapper class, i.e. TRandomMixMax.
We have actually these instance of Mixmax available as TRandom (they are typedef to some templated instances)

TRandomMixMax, TRandomMixMax17 and TRandomMixMax256

see https://root.cern.ch/doc/master/group__Random.html#ga01fce66c4b5b2bb82196539b3c974cec

Lorenzo

Hello,

Thanks for the suggestion Lorenzo.
Unfortunately I’m having some difficulties (on 6.12/04, the newest here). If I simply try from terminal (even after I #include <TRandomGen.h>):

root [0] randgen = new TRandomMixMax(2)
IncrementalExecutor::executeFunction: symbol '_ZN4ROOT4Math12MixMaxEngineILi240ELi0EE4NameB5cxx11Ev' unresolved while linking function '_GLOBAL__sub_I_cling_module_8'!
You are probably missing the definition of ROOT::Math::MixMaxEngine<240, 0>::Name[abi:cxx11]()
Maybe you need to load the corresponding shared library?
(TRandomGen<ROOT::Math::MixMaxEngine<240, 0> > *) nullptr

While with an .L + script works:

#include <TRandomGen.h>
TRandomMixMax* randgen = new TRandomMixMax(2);

Then I can use also from terminal (from script it seems the same, and same error message):

root [1] randgen->Uniform()
(double) 0.42417507
root [2] randgen->Uniform()
(double) 0.86515667
(double) 0.94750995
(double) 0.87527533
root [5] randgen->SetName("randgen")
root [6] randgen->SaveAs("randgenState.root")
Error in <TStreamerInfo::Build>: ROOT::Math::MixMaxEngine<240,0>, unknown type: ROOT::Math::MixMaxEngineImpl<240>* fRng

Info in <TRandomGen<ROOT::Math::MixMaxEngine<240,0> >::SaveAs>: ROOT file randgenState.root has been created
//Next time I open it, I should get these numbers:
root [7] randgen->Uniform()
(double) 0.042604599
(double) 0.46194851
(double) 0.33617805
(double) 0.24434773

I get the same error with new TFile...; ... randgen->Write(). But the file is written, and I can open it back later:

root [0] f = new TFile("randgenState.root", "READ");
root [1] randgen
Error in <TStreamerInfo::Build>: ROOT::Math::MixMaxEngine<240,0>, unknown type: ROOT::Math::MixMaxEngineImpl<240>* fRng

(TRandomGen<ROOT::Math::MixMaxEngine<240, 0> > *) 0x1dc5870
root [2] randgen->Uniform()
(double) 0.26180742
(double) 0.40626434
(double) 0.46813692
(double) 0.53919774

The numbers changed! And the new ones seem to be the same regardless of the initial seed, or how many numbers I generate before saving.

Hi,

Yes, it seems the state of the generator is not saved, for this you get this error in TStreamerInfo::Build.
I will investigate and try to fix this issue.

I cannot however reproduce your first problem. On which computer architecture are you running ?

Lorenzo

I am on Ubuntu 16.04, 64bit, and I downloaded the binaries “root_v6.12.04.Linux-ubuntu16-x86_64-gcc5.4.tar.gz”. I’m not very sure on what info you need, so tell me if you want to know something else.

Thank you for your time!

Thank you for your answer. I will try to reproduce this problem on a Linux Ubuntu machine

Best Regards

Lorenzo

This is still happening on 6.14.02, with both pre-compiled version on unbuntu and self-compiled on CentOS7.

Given that MixMax is now default on CLHEP/G4, would be nice to have access to similar functionality and be able to save the state. It would also be nice to be able to extract the seed(s) using simple data types, too.

1 Like