# Limiting range when sampling normal distribution

**URL:** <https://root-forum.cern.ch/t/limiting-range-when-sampling-normal-distribution/54690>\
**Category:** Newbie\
**Tags:** root\
**Created:** [May 2, 2023, 5:20am UTC](https://root-forum.cern.ch/t/limiting-range-when-sampling-normal-distribution/54690 "2023-05-02T05:20:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javier\_Rufino](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/javier_rufino/32/33185_2.png) [@Javier\_Rufino](https://root-forum.cern.ch/u/Javier_Rufino)\
**Post date:** [May 2, 2023, 5:20am UTC](https://root-forum.cern.ch/t/limiting-range-when-sampling-normal-distribution/54690/1 "2023-05-02T05:20:15Z")

</div>

When sampling a normal distribution is there a way to limit the output, or limit the range of the number generated? For example, if I wanted to simulate a beam that has a gaussian shape I would use 2D gaussian sampling from Gaus():

TRandom3 r(0);  
BeamPositionX = r.Gaus(x, sigma\_x);  
BeamPositionY = r.Gaus(y, sigma\_y);

Will output random values centered at (x,y) and the width of this gaussian is determined by sigma\_x and sigma\_y, but what if I wanted to cut off the range of BeamPositionX and BeamPositionY because the randomly generated outputs are outside the range with respect to the beam window?

I know that I could put a conditional statement:  
if(BeamPositionX \> xUpperLimit || BeamPositionX \< xLowerLimit){repeat sampling}  
But I wanted to avoid repeating these kinds of calculations if possible, and hope that there may be something already implemented that limits this range.

Thanks,  
Javier

\_ROOT Version: 6.26.10  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 2, 2023, 7:18am UTC](https://root-forum.cern.ch/t/limiting-range-when-sampling-normal-distribution/54690/2 "2023-05-02T07:18:48Z")

</div>

Welcome to the ROOT forum.

I guess @moneta can help.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [May 2, 2023, 8:10am UTC](https://root-forum.cern.ch/t/limiting-range-when-sampling-normal-distribution/54690/3 "2023-05-02T08:10:54Z")

</div>

Hi,  
Generating random number according to the normal distribution is quite fast, so if your acceptance range is quite large is still efficient throwing some numbers away applying a conditional statement.  
Otherwise you can also create a TF1 (or TF2) object in a given range and use `TF1::GetRandom`. This operations performs a small approximation, by binning the function, but if you use a not too small binning the result should be fine. Otherwise you can also use `UNURAN` with the `TUnuran` and `TUnuranContDist` class, see the tutorial [unuranDemo.C](https://root.cern.ch/doc/master/unuranDemo_8C.html)

Lorenzo

---

<div class="post-metadata">

**Author:** ![Javier\_Rufino](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/javier_rufino/32/33185_2.png) [@Javier\_Rufino](https://root-forum.cern.ch/u/Javier_Rufino)\
**Post date:** [May 25, 2023, 3:49pm UTC](https://root-forum.cern.ch/t/limiting-range-when-sampling-normal-distribution/54690/4 "2023-05-25T15:49:58Z")

</div>

Thank you! This is exactly what I needed.
