# How does TF1::GetRandom() work?

**URL:** <https://root-forum.cern.ch/t/how-does-tf1-getrandom-work/36031>\
**Category:** Newbie\
**Created:** [September 14, 2019, 1:28pm UTC](https://root-forum.cern.ch/t/how-does-tf1-getrandom-work/36031 "2019-09-14T13:28:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicola\_Rizzi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_rizzi/32/18416_2.png) [@Nicola\_Rizzi](https://root-forum.cern.ch/u/Nicola_Rizzi)\
**Post date:** [September 14, 2019, 1:28pm UTC](https://root-forum.cern.ch/t/how-does-tf1-getrandom-work/36031/1 "2019-09-14T13:28:26Z")

</div>

Hi guys,

can anyone explain to me how `TF1::GetRandom()` exactly works when called?

I don’t know if it’s only me, but the explanation given in the source code is very foggy, or maybe too much condensed.

I will appreciate any kind of help

---

<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:** [September 14, 2019, 5:29pm UTC](https://root-forum.cern.ch/t/how-does-tf1-getrandom-work/36031/2 "2019-09-14T17:29:12Z")

</div>

Hi,  
the `TF1::GetRandom()`of the TF1 extract a number using the TF1 function as a p.d.f.  
In example I defined the TF1 as

`TF1 *f=new TF1("f","[0]*x*exp(-x)",0,20)` and set the parameter [0] equal to 250

then i called 2000 times GetRandom to fill an histogram

`for( int i=0;i<2000;i++)h->Fill(f->GetRandom())`

and the result is the plot attached.(choosing the right scaling factors, the TF1 overlap with the histogram perfectly)

Cheers,  
Stefano

 ![pdf_vsHist](https://root-forum.cern.ch/uploads/default/original/3X/f/4/f4a44452352b2f81f0a0af3c1045e7de9e1478b4.png)

---

<div class="post-metadata">

**Author:** ![Nicola\_Rizzi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_rizzi/32/18416_2.png) [@Nicola\_Rizzi](https://root-forum.cern.ch/u/Nicola_Rizzi)\
**Post date:** [September 14, 2019, 5:53pm UTC](https://root-forum.cern.ch/t/how-does-tf1-getrandom-work/36031/3 "2019-09-14T17:53:43Z")

</div>

Hi Stefano,

Sorry for the misunderstanding, but with my questions I meant: “How does GetRandom() can give you random numbers following the function shape?”

The explanation given in the source file is the following:  
The distribution contained in the function fname (TF1) is integrated over the channel contents.  
It is normalized to 1. For each bin the integral is approximated by a parabola. The parabola coefficients are stored as non persistent data members Getting one random number implies:

- Generating a random number between 0 and 1 (say r1)
- Look in which bin in the normalized integral r1 corresponds to
- Evaluate the parabolic curve in the selected bin to find the corresponding X value.

This is what is not clear to me about GetRandom()

Thank you so much

---

<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:** [September 15, 2019, 8:00am UTC](https://root-forum.cern.ch/t/how-does-tf1-getrandom-work/36031/4 "2019-09-15T08:00:00Z")

</div>

Sorry for the late answer and the misunderstanding,  
When you want to generate a random number according to a specific distribution, you have  
to apply the [Inverse transform sampling](https://en.wikipedia.org/wiki/Inverse_transform_sampling)

- normalize the area of the distribution
- build the [cumulative distribution (CDF)](https://en.wikipedia.org/wiki/Cumulative_distribution_function), basically you integrate the the normalized function.
- the cumulative distribution for how is defined, span the y-axis the range [0,1] and is monotonically increasing
- randomly choose a number r1 between 0 and 1, you choose the y of the CDF, then to this y correspond only one x, and this x is distributed as the function you defined.

In simple case, as exponential distribution you are able to do all this analitically and obtain

```auto
x = log(1-r1)/(−λ)

```

otherwise you have to do everything numerically with some approximation, and it is what root do.

I hope everything is understandable .

S

---

<div class="post-metadata">

**Author:** ![Nicola\_Rizzi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_rizzi/32/18416_2.png) [@Nicola\_Rizzi](https://root-forum.cern.ch/u/Nicola_Rizzi)\
**Post date:** [September 15, 2019, 8:15am UTC](https://root-forum.cern.ch/t/how-does-tf1-getrandom-work/36031/5 "2019-09-15T08:15:22Z")

</div>

Ok that’s clear, but what about the parabola mentioned in the description?

---

<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:** [September 15, 2019, 8:18am UTC](https://root-forum.cern.ch/t/how-does-tf1-getrandom-work/36031/6 "2019-09-15T08:18:40Z")

</div>

It’s just for the approximation of the cumulative and to easily compute the x from the y

---

<div class="post-metadata">

**Author:** ![Nicola\_Rizzi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_rizzi/32/18416_2.png) [@Nicola\_Rizzi](https://root-forum.cern.ch/u/Nicola_Rizzi)\
**Post date:** [September 15, 2019, 8:26am UTC](https://root-forum.cern.ch/t/how-does-tf1-getrandom-work/36031/7 "2019-09-15T08:26:56Z")

</div>

Ok thank you, problem solved 😃
