Problem while generating random numbers

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:

    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.

What’s “zmin” and “zmax”?

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


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.


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