Allow negative values to be generated from a function

Hi rooters,

I am trying to generate random numbers from a specific function. I create a TF1 object and use GetRandom() from there. The problem I am having is that it does have some negative values but it assumes the absolute value: “Warning in TF1::GetRandom: function:f1 has 12 negative values: abs assumed”. I don’t want it to assume abs, I would like the negative values as well. What to do?

Hi,

in general you draw random numbers out of a pdf which is always positive: this is the reason why ROOT behaves like this.
You can multiply the random number by the sign of the function value (std::signbit) at the x corresponding to the generated value (TF1::GetX). It will not be efficient.

Cheers,
D

Hi,

TF1::GetRandom generates the x value for a given f(x). y=f(x) must be a function which can have x negative but y MUST BE positive. There is no other way, if you don’t like the correction ROOT is doing (using abs(y)), you need to modify the function yourself.
But note that you will get negative x values if the function is defined in a range which include x < 0.

Lorenzo