Random Number Generators in CERN ROOT

The most important aspect of a simulation or Monte Carlo Simulation is the random number generator. A truly unbiased generator can make the simulation exactly like real life. There are many types of process to generate a random numbers. You may find easily on the web. So, you can develop your own generator or can use some predefined packages.

CERN ROOT has a package called TRandom. TRandom.h is needed to include in the cpp header. You will find a lot of generators in root which will serve your purpose easily. The most common and useful generator is the Uniform() function. The output of gRandom->Uniform() is a double precision float within the range [0,1] which follows a uniform distribution. This uniform distribution can be used to generate any kind of other distributions using different type of methods – Transformations of Variables, Transformation Law of Probabilities, Method of Rejection and Acceptance and many more. These all are available on the web.

Now, instead of generating your own generators sometimes it is good to use ready-made functions. In ROOT, there are many of them – Exp(tau), Gaus(mean,sigma), Landau(mpv,sigma), Poisson(mean), Binomial(ntot,prob) etc. Just call them using gRandom->XXXX and it will return a float following that distribution.

I have given a example on how to generate random numbers and fill them in 1-D and 2-D Histograms (or TH1F and TH2F). In between, we will also learn how to plot LEGO and COLZ with Th2F. Find the code bellow.

Warning: TH1F (or 1-D Histogram) looks like a TGraph (or 2-D Scatter Plot). And also, TH2F (or 2-D Histogram) looks like a TGraph2D (or 3-D Scatter Plot). But, these are not same. Histograms are entirely different from scatter plot. Learn the difference.

The Code for Histograms

After executing this code, a ps file with two pages containing three histograms will be created. The ps file will look like this.

Histogram PS File

Play with other random number generators. Try to write your own generator using gRandom->Uniform(). Fill the histograms. If you have real data, then use it.

The Gaus(mean,sigma) function is very useful to generate Gaussian Noise to simulate noise in a physics simulation exercise. This is how I have generated the data for the previous two examples (V-I Plots). Just have a look into the code bellow.

Code for Generating V-I Data with Gaussian Noise

Do not think that the whole world around you is a lie or simulation. You have just learned how to cheat in the experimental/practical classes. If you are reading this post, then you will be a master in manipulating data in your laboratory notebook. Or, you can use this technique to verify your result and surprise your teacher. And, if you are a teacher, then teach your students how to cheat. Meanwhile, they will learn simulations too.

Tags: TRandom, TH1, TH2, TPostScript