GetRandom() function with power law distributions

Hi,

I want to generate random numbers following a power law distribution, for what I wrote the next example:

[code]void genrand(Int_t nevnts = 100000, Double_t nbins = 10000., Double_t min = 1., Double_t max = 10000.)
{

TCanvas *c1 = new TCanvas(“c1”,“c1”,900,300);
c1->Divide(3,1);
c1->cd(1);
gPad->SetLogy(); gPad->SetLogx();

TF1 *f1 = new TF1(“f1”, “1./x”, min, max);
//f1->SetRange(1.,10000.);
f1->Draw();

// FillRandom() method
c1->cd(2);
gPad->SetLogy(); gPad->SetLogx();
TH1D *h1 = new TH1D(“h1”,“Random Distribution from FillRandom()”,nbins,min,max);
h1->FillRandom(“f1”,nevnts);
h1->Draw();

// GetRandom() method
TH1D *h2 = new TH1D(“h2”,“Random Distribution from GetRandom()”,nbins,min,max);
Double_t rrd;
for (int i=0; i<nevnts; i++)
{
rrd = f1->GetRandom();
h2->Fill(rrd);
}

c1->cd(3);
gPad->SetLogy(); gPad->SetLogx();
h2->Draw();

cout << " FIN " << endl;
}[/code]

In my results (see attachment) I can see that the function is well defined, and that the first method used (with FillRandom() ) reproduced the function well. But when using GetRandom() function, I get a depth in the function, which position is related with the difference max-min.

I saw some post about the problem appearing when the point 0 is included, but I am excluding it in my example. I even tried to set the range directly f1->SetRange(1.,10000.), but it gave me the same result.

It seems for me that the function has problems to generate random numbers over more than 2 orders of magnitude (with min=1 and max=100 no depth appears).

Has anybody idea what could be the problem?

Thanks in advance,
Estela.


When calling GetRandom on a TF1 function over several orders of magnitude you must increase the number of points for which the integral of the function is computed. In your case I suggest adding “f1->SetNpx(10000)” after the definition of the function as shown below

Rene

[code]void genrand(Int_t nevnts = 100000, Double_t nbins = 10000., Double_t min = 1., Double_t max = 10000.)
{

TCanvas *c1 = new TCanvas(“c1”,“c1”,900,300);
c1->Divide(3,1);
c1->cd(1);
gPad->SetLogy(); gPad->SetLogx();

TF1 *f1 = new TF1(“f1”, “1./x”, min, max);
f1->SetNpx(10000);
//f1->SetRange(1.,10000.);
f1->Draw();

// FillRandom() method
c1->cd(2);
gPad->SetLogy(); gPad->SetLogx();
TH1D *h1 = new TH1D(“h1”,“Random Distribution from FillRandom()”,nbins,min,max);
h1->FillRandom(“f1”,nevnts);
h1->Draw();

// GetRandom() method
TH1D *h2 = new TH1D(“h2”,“Random Distribution from GetRandom()”,nbins,min,max);
Double_t rrd;
for (int i=0; i<nevnts; i++)
{
rrd = f1->GetRandom();
h2->Fill(rrd);
}

c1->cd(3);
gPad->SetLogy(); gPad->SetLogx();
h2->Draw();

cout << " FIN " << endl;
}
[/code]

Perfect, now it works well.

Thank you very much,
Estela.