How fill the random numbers at h1?

I make the random number by using following
TRandom * r = new TRandom();
And I make histogram in order to show that the TRandom operate well.
TH1D *h1 = new THID(“h1”,“Random”,20,-10,10);
Now
for(Int_t i=0; i<10h1 -> Draw();0; ++i){
r -> Uniform(-10.0,10.0);
h1 -> Fill®;
}
But root say there is an error. A few tries later, I found that h1 -> Fill®; is problem. However I don’t know why it is wrong. Could you tell me why it is wrong.


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi,

try this one:

        TRandom* r = new TRandom();
        TH1D *h1 = new TH1D("h1", "Random", 20, -10, 10);
        for(Int_t i=0; i<10000; ++i)
                h1->Fill(r->Uniform(-10.0, 10.0));
        h1->Draw();

Hi Keistad,
r is a TRandom object pointer and cannot be used to fill an histogram.
Instead, when you call r->Uniform(-10.,10.) you are returning a double between -10 and 10.

r -> Uniform(-10.0,10.0);//here you generate a uniform random value between -10 and 10
//but is lost since is not used or assigned
h1 -> Fill(r);// here you try to fill the histogram with a TRandom pointer
//and is an error

This is the reason why the program of yus works fine, because everytime the random number is generated is used to fill the histogram.

another possible way can be define a
double rnd_num and each cycle assign it the value generated from calling Uniform from r
and assign it a value

rnd_num = r -> Uniform(-10.0,10.0);//here you generate a uniform random value between -10 and 10
//and is assigned to rnd_num
h1 -> Fill(rnd_num);//  the histogram is filled with rnd_num

Cheers,
stefano

Thanks for your help. I understand what is problem. Thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.