Home | News | Documentation | Download

TH1D filled by FillRandom is different from the one filled by GetRandom

I have defined a function, “fun”. Based on this function, a histogram is filled by


While, if a histogram is filled by the random number of this function like this,

         for(int i=0;i<1000000;i++){

the histogram h1_sample is different from h1_func.
Does any one know how it happens? Thanks in advance.
BTW, my code is based on ROOT Version: 6.16/00.


It looks like it is the implementation of FillRandom doing something different:

but @moneta can better comment on what.


I think you should get the same results, within statistical lfluctuations, if you set the number of function bins equal to the histogram ones:
func->SetNpx( h1_func->GetNbinsX())

In case of different bins, especially of the function bins are less than the histogram one you could get a significative different result. The default number of function bins is 100 .


Thank you very much.
I have tried to set the number of points to a very large value, but the result is the same.
I also generated a seriese of random numbers based on this function using an analytical way. This time the obtained histogram is consistent with the one filled by func->GetRandom(). My code is like this,test.cc (2.4 KB)

And the comparison of these three histograms is shown in the attached figure.comp.pdf (57.7 KB)


You need compatible bins between the TF1 and TH1 to have same result. In addition, TF1::GetRandom and TH1::FillRandom generate only in the range, while your analytical solution no. So there is a normalisation issue.
I have modified your program and now the three methods show compatible results

test.cc (2.5 KB)

Hi, Moneta
Thank you so much. It works with the modified program.
I was wrong when using the TH1 FillRandom method before.
FillRandom(“func”,Nsample) only generates randoms numbers within the range of TH1, not TF1.
Thanks again. This problem is solved.