Program

/* random walk simulation */
 
#include <stdio.h>
#include <math.h>
 
#define max 1000			/* number of steps */
#define seed 11168			/* seed for number generator */
#define SQRT_2    1.4142135623730950488

FILE *output;				/* internal file name */

main()
{
int i,j;
double x, y, r[max+1];

output= fopen ("walk.dat", "w");	/* external file name */

for (i=0; i<=max; i++) r[i]=0;		/* clear array */

srand48(seed);				/* seed the number generator */

for (j=1; j<=100; j++)			/* average over 100 trials */
{
   x=0; y=0;				/* starting point */

   for (i=1;i<=max; i++)
      {
         x += (drand48()-0.5)*2.0*SQRT_2;	/* numbers between */ 
         y += (drand48()-0.5)*2.0*SQRT_2;	/* -sqrt(2) and sqrt(2) */
          
         r[i]+=sqrt(x*x+y*y);
      }
}
         /* write results into file */
for (i=0; i<=max; i++) fprintf(output,"%f\t%f\n", sqrt((double)(i)), r[i]/100.0);

fclose (output);
}

A DEC C source and a ANSI-C source which you can save and run on your computer.
Back to main document.