# GetRandom() not working for peak functions

**URL:** <https://root-forum.cern.ch/t/getrandom-not-working-for-peak-functions/51304>\
**Category:** Newbie\
**Created:** [August 24, 2022, 8:02am UTC](https://root-forum.cern.ch/t/getrandom-not-working-for-peak-functions/51304 "2022-08-24T08:02:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erich\_Leistenschneid](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/erich_leistenschneid/32/30671_2.png) [@Erich\_Leistenschneid](https://root-forum.cern.ch/u/Erich_Leistenschneid)\
**Post date:** [August 24, 2022, 8:02am UTC](https://root-forum.cern.ch/t/getrandom-not-working-for-peak-functions/51304/1 "2022-08-24T08:02:26Z")

</div>

Hi y’all,

I think I may be doing something stupid with TRandom.  
I am trying to build a quite complex distribution of random numbers composed of several Lorentzians on top of a polynomial background.  
However, the output of GetRandom() seems to be getting weird distributions for any peak function.

For example, the code below should generate a gaussian, but instead is generating this:

 ![c1](https://root-forum.cern.ch/uploads/default/original/3X/b/b/bb45a3b33323ab24664c95a74f12270043a67697.jpeg)

Is that any obvious what I am doing wrong?

```auto
Double_t VelocityDistribution(Double_t *f, Double_t *par)
{
	double xc,w;
	xc = par[0];
	w = par[1];
	double x = f[0];

  // return TMath::BreitWigner(x,xc,w);
    return TMath::Gaus(x,xc,w);
}

void vel(){
	int c;
	TH1* h1 = new TH1F("h1", "Velocity Distribution", 10000, 80.0, 100.0);
	TF1 *f1 = new TF1("ionvelocity",VelocityDistribution,0.0,165.0,2);
	
	f1->SetParameter( 0 , 90.0 );
	f1->SetParameter( 1 , 0.09 );
	 
	for(c=0;c<1000000;c++)
		h1->Fill(f1->GetRandom());

	TCanvas *c1 = new TCanvas("c1","c1",1000,1000);
	h1->Draw();
}

```

_ROOT Version:_ 6.24/06  
_Platform:_ Windows 10 pro

* * *

---

<div class="post-metadata">

**Author:** ![joa](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joa](https://root-forum.cern.ch/u/joa)\
**Post date:** [August 24, 2022, 8:11am UTC](https://root-forum.cern.ch/t/getrandom-not-working-for-peak-functions/51304/2 "2022-08-24T08:11:28Z")

</div>

Hi,

this is a bug I would say. If you add

f1-\>SetNpx(10000);

before the for loop it works. This tells root how often to sample the function when plotting it. But apparently also when integrating etc.

cheers

Joa

---

<div class="post-metadata">

**Author:** ![Erich\_Leistenschneid](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/erich_leistenschneid/32/30671_2.png) [@Erich\_Leistenschneid](https://root-forum.cern.ch/u/Erich_Leistenschneid)\
**Post date:** [August 24, 2022, 8:15am UTC](https://root-forum.cern.ch/t/getrandom-not-working-for-peak-functions/51304/3 "2022-08-24T08:15:46Z")

</div>

Ha! This would make sense indeed!  
Thank you!

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [August 24, 2022, 8:28am UTC](https://root-forum.cern.ch/t/getrandom-not-working-for-peak-functions/51304/4 "2022-08-24T08:28:22Z")

</div>

Hi @Erich_Leistenschneid and welcome on the root forum.

In order to compute a random number from a generic function ROOT need to compute the cumulative function.

[Here](https://root.cern.ch/doc/master/TF1_8cxx_source.html#l02259) there is how the function works.

Have also a look to [TRandom](https://root.cern.ch/doc/master/classTRandom.html) there are already built in fucntion to generate random number from Breit-Wigner or Gaussian distribution.

Best
