Generating random numbers according to user-defined function

Dear ROOTers,

To generate 2 uncorrelated random numbers from a two-dimensionnal Gaussian one simply do:

TRandom r;
float x=r.Gaus(0,1);
float y=r.Gaus(0,1);

I want to generate correlated random numbers, so rather something like

vector<double> vect;
vect = r.SomeCorrelatedGaussian();
x = vect[0];
y = vect[1];

I could define a pdf and use RooMCStudy, but is’nt there a way with TRandom class ?



Hi Xavier,

In RooFit, the easiest way to do this is to use class RooMultiVariateGaussian, which
implement a n-D Gaussian distribution where you can specify the full covariance matrix,

RooRealVar x(“x”,“x”,-10,10) ;
RooRealVar y(“y”,“y”,-10,10) ;

TVectorD mu(2) ;
mu(0) = 0 ; mu(1) = 0 ;

TMatrixDSym V(2) ;
V(0,0)=9 ; V(0,1=6 ;
V(1,0)=6 ; V(1,1)=9 ;

RooMultiVarGaussian mvg(“mvg”,“mvg”,RooArgList(x,y),mu,V) ;
RooDataSet* d = mvg.generate(RooArgSet(x,y),1000) ;

Class RooMultiVarGaussian has internal optimized generator for correlated
Gaussian event generation so it should be pretty fast.


see also TF1::GetRandom and TF2::GetRandom