Random Number Generation

I had a few questions about generating random numbers from TF1s in ROOT.

First, there look to be two ways to generate these numbers. One can define a TF1 and call TF1::GetRandom() or instantiate a TUnuran object with TF1. Is there a speed benefit to either method (or does TF1::GetRandom() use TUnuran deep down)?

Secondly, if I don’t provide bounds on the TF1 it seems to automatically set the bounds to (0, 1) which means TF1::GetRandom() is limited to (0, 1). Is there a way to set the bounds to +/- infinity without using an arbitrarily large numbers for bounds?


TF1::GetRandom() is an approximate method and works for a bounded region. You can tune by using TF1::SetNpx() to increase the number of points used, but you will decrease the efficiency

Unuran instead provide exact methods, which work for both limited and unlimited distributions and they can be in some cases even more efficient.

Best Regards


Addendum to Lorenzo’s answer:
You can specify the function range in the TF1 constructor. By default GetRandom will return a number in the specified range.



Many thanks. Exactly the kind of information for which I was looking.


I’m aware of the constructor options, but I would effectively like to set them to -infinity and infinity. Is this possible without using arbitrary large numbers?

Also, do the bounds of the TF1 extend to Unuran when TUnuran::Init(TUnuranDistrCont(TF1 f)) is used?

Thanks for the help.

To set the bound in Unuran you have to call the method


It does not extend automatically those from TF1 because those refer normally to the plotting domain and not the function domain.



Ah, so TF1::Eval(x) returns a valid answer even outside of the bounds (xmin,xmax) set in the constuctor?

In this case, what are the default bounds in TUnuranDistrCont?

Thanks again.

Yes, in general TF1::Eval(x) returns a valid answer also outside the bounds.

No bounds are defined by default in the unuran class TUnuranDistrCont, i.e. they are (-inf,+inf).

Awesome. Thanks so much for you help.

Apologies for one more question. I’m trying to use TUnuran for sampling in compiled code. I have no problems with TUnuran itself, but I can’t seem to find the appropriate header for TUnuranDistrCont.

I have a fink install on Mac OS X 10.4, and all I can find is TUnuranDistr.h which doesn’t seem to help. Any help would be appreciated.