# Creating random cosine angles

Hi dear experts,

I have plotted a 2D histogram from .txt file and now I have to generate a random cosine angle…

``````void CDF()
{
TRandom2 *rand = new TRandom2(1);
TCanvas *c1 = new TCanvas();
TH1F *histogram = new TH1F("histogram ", " ", 10001, -1, 1);
TH2F *hist = new TH2F("hist"," Histogram ", 10001, -1.05, 1.1, 10001, 0.0000003, 1.);
const int length = 10001;
double mu[length], f[length];
double rr = 0;
double indm = f[length];
double M = mu[length];

fstream file;
file.open ("dSigma-dOmega.txt", ios::in);

int loop=0;
while(!file.eof())

{

file >> mu[loop] >> f[loop];
rr= rand-> Rndm();
//hist->Fill(mu, f);
//   cout<<loop<<" "<<mu[loop]<<" "<<f[loop]<<" "<<rr<<endl;
loop++;
histogram->Fill(rr);

}

hist->GetXaxis()->SetTitle("mu");
hist->GetXaxis()->CenterTitle(true);
hist->GetYaxis()->SetTitle("f");
hist->GetYaxis()->CenterTitle(true);

c1->SetLogy();

hist->SetMarkerColor(35);
hist->Draw();
histogram->Draw();

file.close();
}
``````

To complete my code I must write Wolfram Mathematica code for C++
Wolfram Mathematica code here …

``````While[(M = Quotient[M, 10]) != 0, While[f[[indm -= M]] > rr];
indm += M];
fi = f[[indm - 1]];
[mu]i = [mu][[indm - 1]];
m = (rr - fi) ([mu][[indm]] - [mu]i)/(f[[indm]] - fi) + [mu]i
``````

Best regards,

ROOT Version: 6
Platform: Ubuntu20.10
Compiler: Not Provided

Hi @Eyvaz ,
and welcome to the ROOT forum!

Can you please elaborate more on what exactly is not working for you? To generate a random angle it should be enough to generate a random number, possibly restrict it between 0 and 360 degrees (and then evaluate its cosine)?

Cheers,
Enrico

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.