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?
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);
[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]