reolynda 0 Newbie Poster

// so guys this how i program about random walk of partilce..somehow... R ~ N^1/2
i got a huge different comparing simulation value and calculation(theoritical) .how i wanna to reduce different i got,,,/ N=number of steps... R= value from therom phitogoras R=(a^2 + b^2)^1/2.....

#include <iostream>
#include <math.h>
#include <time.h>

using namespace std;
main()
{
  //initialization
  float count=0.0;
  int i,j,k,y,part;
  int n= 100;
  float f=0.0;
  float a[n];
  float b[n];
  float c[n];
  float d[n];
  float z[n];
  float u[n];
  float pe=0.0;
  float jumlah=0.0;
  float diffx[n];
  float diffy[n];
  float p[n];
  float h[n];
  float cordx[n];
  float cordy[n];
  float steps[n];
  srand ((unsigned)time(0));    //random number generator

  //initialize for particle in cord x and cord y
  

      for (k=0;k<n;k++)
	{
	  cordx[k]=rand()%n;
	  p[k]=cordx[k]; //store initial condition of x
	  cordy[k]=rand()%n;
	  h[k]=cordy[k]; //stor initial condition of y
	}
      for(i=0; i<n; i++)
	{
	  steps[i]=0;
	}
      
      //operation start 
      
      for (j=0;j<1000000;j++)
	{
	  
	  part=rand()%n;
	  steps[part]++;

	  if(cordx[part]==0 && cordy[part]==0)
	    {
	      //cout<<"A:Initial "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	      y=rand()%2;
	      if(y==0)cordx[part]=cordx[part]+1;
	      if(y==1)cordy[part]=cordy[part]+1;
	      //cout<<"A:Final "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	    }

	  else  if(cordx[part]!=0&& cordy[part]==0)
	    
	    {
	      //cout<<"B:Initial "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	      y=rand()%2;
	      if(y==0)cordx[part]=cordx[part]+1;
	      if(y==1)cordx[part]=cordx[part]-1;
	      if(y==2)cordy[part]=cordy[part]+1;
	      // cout<<"B:Final "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	    }	  
	  
	  
	  else if(cordx[part]==(n-1)&& cordy[part]==0)
	    {
	      //cout<<"c:Initial "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	      y=rand()%3;
	      if(y==0)cordx[part]=cordx[part]-1;
	      if(y==1)cordy[part]=cordy[part]+1;
	      //cout<<"C:Final "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	    }
	  
	  
	  else if(cordx[part]==0&&  cordy[part]!=0)
	    {
	      //cout<<"D:Initial "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	      y=rand()%3;
	      if(y==0)cordx[part]=cordx[part]+1;
	      if(y==1)cordy[part]=cordy[part]-1;
	      if(y==2)cordy[part]=cordy[part]+1;
	      //cout<<"D:final "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	    }
	  
	 
	  else if(cordx[part]==(n-1) && cordy[part]!=0)
	    {
	      //cout<<"F: FInal "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	      y=rand()%3;
	      if(y==0)cordx[part]=cordx[part]-1;
	      if(y==1)cordy[part]=cordy[part]+1;
	      if(y==2)cordy[part]=cordy[part]-1;
	      //cout<<"F:Final "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	    }
	  
	  
	  else if(cordx[part]==0 && cordy[part]==(n-1))
	    {
	      //cout<<"G:Initial "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	      y=rand()%2;
	      if(y==0)cordx[part]=cordx[part]+1;
	      if(y==1)cordy[part]=cordy[part]-1;
	      // cout<<"G:Final "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	    }
	  
	  
	  else if(cordx[part]!=0&& cordy[part]==(n-1))
	    {
	      //cout<<"H:Initial "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	      y=rand()%3;
	      if(y==0)cordx[part]=cordx[part]+1;
	      if(y==1)cordy[part]=cordy[part]-1;
	      if(y==2)cordy[part]=cordy[part]-1;
	      // cout<<"H:Final "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	    }
	  
	  
	  else if(cordx[part]==(n-1) && cordy[part]==(n-1))
	    {
	      //cout<<"I:Initial "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	      y=rand()%2;
	      if(y==0)cordx[part]=cordx[part]-1;
	      if(y==1)cordy[part]=cordy[part]-1;
	      // cout<<"I:Final"<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	    }
	  
	  
	  else
	    {
	      //cout<<"J:Initial "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	      y=rand()%4;
	      if(y==0)cordx[part]=cordx[part]+1;
	      if(y==1)cordx[part]=cordx[part]-1;
	      if(y==2)cordy[part]=cordy[part]+1;
	      if(y==3)cordy[part]=cordy[part]-1;
	      //cout<<"J:Final "<<cordx[part]<<"\t "<<cordy[part]<<"\n";
	    }
	  
	}
  
	  for(k=0; k<n; k++)
	    {
	      diffx[k]=cordx[k]-p[k];
	      diffy[k]=cordy[k]-h[k];
	    }
	  for(k=0; k<n; k++)
	    {
	      a[k]=pow(diffx[k],2);
	      b[k]=pow(diffy[k],2);
	      c[k]=a[k]+b[k];
	      z[k]=sqrt(c[k]);	  	    
	    }
	  for(k=0; k<n; k++)
	    {
	      u[k]=sqrt(steps[k])-z[k];
	      pe=pe+u[k];
	      cout<<z[k]<<"\t"<<sqrt(steps[k])<<"\t" <<sqrt(steps[k])-z[k]<<"\n";
	     
	    }
	   cout<<pe/n<<"\n";
	  for(k=0; k<100; k++)
	  {
	  jumlah=jumlah+z[k];
	  }
  // output data //
	  // cout<<"moving particle"<<"  "<<jumlah<<"\n";

  return(0);
}