#include "nr3.h" #include "ran.h" int main() { // random walk // test the probability that 2 persons find eachother, in a 10 by 10 grid. // they find eachother if they arrive at the same square. // they move 1 step in random direction every even second, (person 1) and odd // second (person 2) // test the average time it takes for an encounter const int Ntrial = 1000000; double steps=0; // average and mean number of time steps needed double stepssqr=0; Ran ran1(1234567); for (int i=0; i 0) { trialstep+=1; if (trialstep%2) { double z=ran1.doub(); if (z<0.25) x1=((x1+1)%10); // step to right else if (z<0.5) x1=(x1+9)%10; // step to left else if (z<0.75) y1=(y1+9)%10; // step down else y1=(y1+1)%10; // step up } else { double z=ran1.doub(); if (z<0.25) x2=((x2+1)%10); // step to right else if (z<0.5) x2=(x2+9)%10; // step to left else if (z<0.75) y2=(y2+9)%10; // step down else y2=(y2+1)%10; // step up } } steps+=trialstep; stepssqr+=trialstep*trialstep; } cout << "Ntrials: " << Ntrial << endl; cout << "average number of steps : " << steps/Ntrial; cout << " RMS : " << sqrt(stepssqr/Ntrial-steps*steps/Ntrial/Ntrial) << endl; return 0; }