# Problem while generating random numbers

**URL:** <https://root-forum.cern.ch/t/problem-while-generating-random-numbers/35923>\
**Category:** Newbie\
**Created:** [September 7, 2019, 5:38pm UTC](https://root-forum.cern.ch/t/problem-while-generating-random-numbers/35923 "2019-09-07T17:38:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnyBoy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/johnyboy/32/19741_2.png) [@JohnyBoy](https://root-forum.cern.ch/u/JohnyBoy)\
**Post date:** [September 7, 2019, 5:38pm UTC](https://root-forum.cern.ch/t/problem-while-generating-random-numbers/35923/1 "2019-09-07T17:38:09Z")

</div>

Hello everyone.  
My issue is the following. I am trying to generate random numbers for a physics simulation. I need to simulate at least 1 million events. But the random numbers have to be distributed according to the following expression:

((1+z)\*sqrt((1+z)^{3}\*0.3 + 0.7))^{-1}

So, I tried this:

```auto
    TF1 *f1 = new TF1("f1","1./((1+x)*sqrt((1+x)*(1+x)*(1+x)*0.3 + 0.7))",zmin, zmax);
    double Zini = f1->GetRandom();

```

On doing this, the programs halts at around 8\*10^{5} events and the computer needs to be restarted, because it freezes. I would thank any piece of advice on what I am doing wrong.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 7, 2019, 6:45pm UTC](https://root-forum.cern.ch/t/problem-while-generating-random-numbers/35923/2 "2019-09-07T18:45:18Z")

</div>

What’s “zmin” and “zmax”?

---

<div class="post-metadata">

**Author:** ![JohnyBoy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/johnyboy/32/19741_2.png) [@JohnyBoy](https://root-forum.cern.ch/u/JohnyBoy)\
**Post date:** [September 7, 2019, 11:17pm UTC](https://root-forum.cern.ch/t/problem-while-generating-random-numbers/35923/3 "2019-09-07T23:17:36Z")

</div>

Two floating point numbers. They are generally set to 0 and 1 respectibly.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [September 8, 2019, 6:16am UTC](https://root-forum.cern.ch/t/problem-while-generating-random-numbers/35923/4 "2019-09-08T06:16:43Z")

</div>

Hi,

You can create the TF1 once, and then call GetRandom() as often as you want on the same object. Do NOT call `new TF1` a million times without calling delete, or you will run out of memory. That’s how C++ works, nothing ROOT specific here.

Axel

---

<div class="post-metadata">

**Author:** ![JohnyBoy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/johnyboy/32/19741_2.png) [@JohnyBoy](https://root-forum.cern.ch/u/JohnyBoy)\
**Post date:** [September 8, 2019, 12:28pm UTC](https://root-forum.cern.ch/t/problem-while-generating-random-numbers/35923/5 "2019-09-08T12:28:10Z")

</div>

Thanks. That solved it. I cant believe I made that mistake. Thanks to everyone for your help.
