/* 2次元ランダムウォーク */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAX_PARTICLES 1000

main()
{
  unsigned int seed;
  int nran;
  int i,j;
  int istep,nstep;
  float a1;
  float x[MAX_PARTICLES],y[MAX_PARTICLES];
  float h;
  FILE *fp;

  printf("Input: seed\n");
  scanf("%u", &seed);
  printf("Number of particles:\n");
  scanf("%u", &nran);
  printf("Input: Number of steps:\n");
  scanf("%u", &nstep);

  srand(seed);

  fp = fopen("2D.dat", "w");

  h=1.0;

  for(i=0; i < nran; i++){
    x[i]=0.0;
    y[i]=0.0;
    for(istep=0; istep < nstep; istep++){
      j=rand();
      a1=j/(RAND_MAX+1.0);
      if(a1<0.25)
	x[i]=x[i]-h;
      else if(a1<0.5)
	x[i]=x[i]+h;
      else if(a1<0.75)
	y[i]=y[i]-h;
      else
        y[i]=y[i]+h; 

    }
    printf("x=%.5f,y=%.5f\n", x[i], y[i]);    
    fprintf(fp, "%.5f   %.5f\n", x[i], y[i]);    
  } 

  return;
}