/* 2次元ランダムウォーク */ #include #include #include #define MAX_PARTICLES 10000 /* #define PI 3.1415926 */ main() { unsigned int seed; int nran; int i,j; int istep,nstep; float a1; float x[MAX_PARTICLES],y[MAX_PARTICLES]; float h; double theta; 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); theta=2*PI*a1; x[i]=x[i]+h * cos(theta); y[i]=y[i]+h * sin(theta); } printf("x=%.5f,y=%.5f\n", x[i], y[i]); fprintf(fp, "%.5f %.5f\n", x[i], y[i]); } return; }