Created
August 24, 2014 11:08
-
-
Save cvson/453fe246e3644f1459bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// randomWalk.C | |
//////////////////////////////////////////////////// | |
// | |
// Simple Monte Carlo | |
// | |
/////////////////////////////////////////////////// | |
// Created by @visionlib.postach.io | |
{ | |
const Int_t kIteration = 1000000; | |
TFile *pfile = new TFile("randomWalk.root","RECREATE"); | |
TH1F *hx10 = new TH1F("hx10","",11,-0.5,10.5); | |
TH1F *hy10 = new TH1F("hy10","",11,-0.5,10.5); | |
TH1F *hx20 = new TH1F("hx20","",11,-0.5,10.5); | |
TH1F *hy20 = new TH1F("hy20","",11,-0.5,10.5); | |
TH1F *hstep = new TH1F("hstep","",100,0,500); | |
TH2F *hstepvsdist = new TH2F("hstepvsdist","",163,-0.5,162.5,100,0,500); | |
TH2F *hstepvsdistsqr = new TH2F("hstepvsdistsqr","",100,-0.5,13.5,100,0,500); | |
//percentage | |
TH1F *hstepLeft1 = new TH1F("hstepLeft1","",100,0,1); | |
TH1F *hstepRight1 = new TH1F("hstepRight1","",100,0,1); | |
TH1F *hstepUp1 = new TH1F("hstepUp1","",100,0,1); | |
TH1F *hstepDown1 = new TH1F("hstepDown1","",100,0,1); | |
TH1F *hstepLeft2 = new TH1F("hstepLeft2","",100,0,1); | |
TH1F *hstepRight2 = new TH1F("hstepRight2","",100,0,1); | |
TH1F *hstepUp2 = new TH1F("hstepUp2","",100,0,1); | |
TH1F *hstepDown2 = new TH1F("hstepDown2","",100,0,1); | |
int nleft1=0; | |
int nright1=0; | |
int nup1=0; | |
int ndown1=0; | |
int nleft2=0; | |
int nright2=0; | |
int nup2=0; | |
int ndown2=0; | |
for (Int_t i=0; i<kIteration;i++) { | |
if (i%1000==0) printf("Processing %i/%i\n",i,kIteration); | |
//Initialize position | |
int trialstep=0; | |
int x1 = (int)(gRandom->Uniform(0.0,1.0)*10); | |
int y1 = (int)(gRandom->Uniform(0.0,1.0)*10); | |
int x2 = (int)(gRandom->Uniform(0.0,1.0)*10); | |
int y2 = (int)(gRandom->Uniform(0.0,1.0)*10); | |
hx10->Fill(x1); | |
hy10->Fill(y1); | |
hx20->Fill(x2); | |
hy20->Fill(y2); | |
double chooseStep; | |
int distant0 = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); | |
nleft1=0; | |
nright1=0; | |
nup1=0; | |
ndown1=0; | |
nleft2=0; | |
nright2=0; | |
nup2=0; | |
ndown2=0; | |
while ((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) > 0) { | |
trialstep +=1; | |
//first person step in even step | |
if (trialstep%2){ | |
chooseStep = gRandom->Uniform(0.0,1.0); | |
if (chooseStep<0.25) {x1 = ((x1+1)%10);//step to right | |
nright1 +=1; | |
} | |
else if (chooseStep<0.5) {x1=(x1+9)%10; // step to left | |
nleft1 +=1; | |
} | |
else if (chooseStep<0.75) {y1=(y1+9)%10; // step down | |
ndown1 +=1; | |
} | |
else {y1=(y1+1)%10; // step up | |
nup1 +=1; | |
} | |
} | |
//second person step in odd step | |
else { | |
chooseStep = gRandom->Uniform(0.0,1.0); | |
if (chooseStep<0.25) {x2 = ((x2+1)%10);//step to right | |
nright2 +=1; | |
} | |
else if (chooseStep<0.5) {x2=(x2+9)%10; // step to left | |
nleft2 +=1; | |
} | |
else if (chooseStep<0.75) {y2=(y2+9)%10; // step down | |
ndown2 +=1; | |
} | |
else {y2=(y2+1)%10; // step up | |
nup2 +=1; | |
} | |
}//end else | |
}//end while | |
hstep->Fill(trialstep); | |
hstepvsdist->Fill(distant0,trialstep); | |
int nstep1 = nright1+nleft1+nup1+ndown1; | |
int nstep2 = nright2+nleft2+nup2+ndown2; | |
if(nstep1>0){ | |
hstepRight1->Fill(nright1*1.0/nstep1); | |
hstepLeft1->Fill(nleft1*1.0/nstep1); | |
hstepUp1->Fill(nup1*1.0/nstep1); | |
hstepDown1->Fill(ndown1*1.0/nstep1); | |
} | |
if(nstep2>0){ | |
hstepRight2->Fill(nright2*1.0/nstep2); | |
hstepLeft2->Fill(nleft2*1.0/nstep2); | |
hstepUp2->Fill(nup2*1.0/nstep2); | |
hstepDown2->Fill(ndown2*1.0/nstep2); | |
} | |
}//end for | |
pfile->Write(); | |
pfile->Close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment