/* 2次元ランダムウォーク */ #include #include #include #define MAX_STEPS 10000 #define PI 3.1415926 main() { unsigned int seed; int i,j; int nstep; float a1; float x[MAX_STEPS],y[MAX_STEPS]; float xp, yp; float h; double theta; FILE *fp; printf("Input: seed\n"); scanf("%u", &seed); printf("Input: Number of steps:\n"); scanf("%u", &nstep); srand(seed); fp = fopen("2D.dat", "w"); h=1.0; xp=0.0; yp=0.0; x[0]=xp; y[0]=yp; fprintf(fp, "%.5f %.5f\n", x[0], y[0]); for(i=0; i < nstep; i++){ j=rand(); a1=j/(RAND_MAX+1.0); theta=2*PI*a1; xp=xp+h * cos(theta); yp=yp+h * sin(theta); x[i+1]=xp; y[i+1]=yp; printf("x=%.5f,y=%.5f\n", x[i+1], y[i+1]); fprintf(fp, "%.5f %.5f\n", x[i+1], y[i+1]); } return; }