{ // define a function f1, a sum of 2 gaussians f(x) = Gaus1(x) + Gaus2(x) TF1* f1 = new TF1("f1", "[0]*TMath::Gaus(x,[1],[2])+[3]*TMath::Gaus(x,[4],[5])", 0, 10); // give the parameters for parameters [0], [1] ... [5], in the same order f1->SetParameters(0.3, 3,0.7, 0.5, 5,0.7); // draw the function f1->Draw(); // define a random number, with initial random seed 1234 TRandom3 rnd(1234); // define a histogram from 0 to 1.5 TH1D* h1 = new TH1D("h1", "h1", 150,0,1.5); // fill the histogram from the random number range from (0, 1) for (int i = 0; i<10000; i++){ // fill the random number, repeat 10000 times, and fill them one-by-one into the histogram h1 h1->Fill(rnd.Rndm()); } // draw the histogram h1->Draw(); // define another histogram to store the generated random number x TH1D* h2 = new TH1D("h2", "h2", 100,0,8); // generate random numbers x for (int i=0; i<10000; i++){ // first generate random number x, range from 0 to 1 double x = rnd.Rndm(); // scale random x from (0,1) ==> to (0, 8) x = x*8; // second, generate another random number y double y = rnd.Rndm(); // scale it to 0 to 0.51 y = y*0.51; // get the function value at x double fx = f1->Eval(x); // check if y < fx, if yes, fill the random number x to h2 if ( y < fx ) { h2->Fill(x); } } // draw h2 h2->Draw("e1"); // transformation method for exp function // define the (1/t)*exp(-x/t) function TF1* f2 = new TF1("f2", "1/[0]*exp(-x/[0])", 0,5); // give parameter [0] the value 0.8 f2->SetParameter(0, 0.8); // define another histogram to store the histogram TH1D* h3 = new TH1D("h3", "h3", 50, 0, 5); // generate random number for( int i=0; i<10000; i++) { //generate random r from 0 to 1 double r = rnd.Rndm(); // calculate random variable x double x = -0.8*log(1-r); // fill it into histogram h3->Fill(x); } // draw h3 h3->Draw(); }