TH1:GetRandom() == same events all the time

Hey rooters,

I am trying to draw a random sample of events from a parent distribution given by a histogram I have previously generated. I am using the TH1:GetRandom() call.

I find that I am however getting the same events every time I run the code. I am calling gRandom->SetSeed(0) at the very start of the code.

Can anyone explain how to fix this.

Is there a way to use the TRandom1 generator for the TH1:GetRandom() process?

Cheers

Andrew

1 Like

At the beginning of your job do

gRandom = new TRandom3(0); see of of TRandom3 constructor

Rene

1 Like

Hi,

What version of Root are you using? If it’s too old, it doesn’t use TRandom3, by default.

With Root 5.20 on Cygwin, it works as expected:

cplager@tableau> root -l
root [0] gRandom->SetSeed(0)   
root [1] gRandom->Uniform (0,1)
(Double_t)6.61601717816665769e-01
root [2] .q
cplager@tableau> root -l
root [0] gRandom->SetSeed(0)   
root [1] gRandom->Uniform (0,1)
(Double_t)1.55551370698958635e-01
root [2] .q
cplager@tableau> root -l
root [0] gRandom->SetSeed(0)   
root [1] gRandom->Uniform (0,1)
(Double_t)2.53423421643674374e-01
root [2] .q
cplager@tableau> root -l
root [0] gRandom->SetSeed(1)   
root [1] gRandom->Uniform (0,1)
(Double_t)4.17021998437121511e-01
root [2] .q
cplager@tableau> root -l
root [0] gRandom->SetSeed(1)   
root [1] gRandom->Uniform (0,1)
(Double_t)4.17021998437121511e-01
root [2] .q
cplager@tableau> root -l
root [0] gRandom->SetSeed(1)   
root [1] gRandom->Uniform (0,1)
(Double_t)4.17021998437121511e-01
root [2] .q

So I wouldn’t think that you’d need to create a new TRandom3 object. (If you do, delete the old one first).

Cheers,
Charles

1 Like

Dear all,
I am using root Version 5.34/20
If I use the following lines from root command line:
TH1D* K = (TH1D*) _file0->Get(“KF_MC_Signal_tree”);
TH1D* K3 = new TH1D(“K3”,"",100,0.45,1.45);
for (int i=0; i<int(K->GetSumOfWeights()); i++) K3->Fill(K->GetRandom());
I get different K3 histograms every time I repeat the filling

while if I use the same code in a C++ compiled code, I always get the same histogram despite the initial
Random seed changes each time.

It seems to me that the C++ the random generator is used by GetRandom not the TRandom3 I initialised
TRandom3 r(0);
UInt_t seed=r.GetSeed() ;
cout<<" -------------> seed="<<seed<<endl;

TH1D *kk_histo = (TH1D*) f_Khisto->Get("KF_MC_Signal_tree");
TH1D* k_histo = new TH1D("KF_MC_Signal_tree_Rndm","randomized KF signal",kk_histo->GetNbinsX(),kk_histo->GetXaxis()->GetXmin(),kk_histo->GetXaxis()->GetXmax());
cout<<" First random "<<  r.Uniform(0.,1.)<<" "<<kk_histo->GetRandom()<<endl;

for (int ii=0; ii<int(kk_histo->GetSumOfWeights() ); ii++){
  k_histo->Fill(kk_histo->GetRandom());
}

both seed and r.Uniform(0.,1) changes each time, while the kk_histo->GetRandom() returns always the same number.
Can you help?
Thanks
Stefania

Do not create your own random engine. The default one is a TRandom3. Try:
gRandom->Print();
So, in the beginning of your macro, it’ll be enough to say:
gRandom->SetSeed(0);

Hi,

If you are using TH1::GetRandom, you need to do, as indicated in the post before,

gRandom = new TRandom3(0); 

since TH1::GetRandom uses gRandom and not the local instance of TRandom3 that you initialise in your code.

Lorenzo

[quote=“Wile E. Coyote”]Do not create your own random engine. The default one is a TRandom3. Try:
gRandom->Print();
So, in the beginning of your macro, it’ll be enough to say:
gRandom->SetSeed(0);[/quote]

Thanks it works now
Stefania