How to generate random numbers according to the 2D probability density function

HI everyone!
I am new to Geant4 and i am trying to build a simple proton pencil beam model.A parameterization of the source model at the source plane is therefore required, including the spatial beam,spread distribution (beam spot size), σx, and the angular spread distribution (beam divergence), σxθ, as well as the coefficient of correlation ρx (the same relation holds for the y‐direction).The 2D Gaussian probability density function of x and θ is(i know the values of σx、σxθ and ρx):

BUT I don’t know how to generate random numbers according to the two-dimensional probability density function in geant4? Does anyone know?
Thank you very much!!!

The easiest way to generate a 2-D correlated gaussian beam distribution is to

  1. Rebuild Geant4 with external CLHEP (and not the built in the standard version)
  2. Generate x and \theta using RandMultiGauss or similar from CLHEP

If you have a accelerator application, take a look at BDSIM which is an extension tool kit for G4 which has all of this already implemented.


Thank you so much! This problem has been bothering me for days. I’ll try your method!

Dear @sboogert ,

I have tabulated a 3D source distribution with a probability density function P(r, theta, phi, E), referring to a neutron with direction (theta,phi), energy E and position r. I wanna generate neutrons as a primary source, accordingly to P(r, theta, phi, E). Also in this case you suggest to use CLHEP or you have other suggestions?
Thanks in advance for your time.
Best regards,