// PROGRAM random_walk2 // 2次元のランダムウォーク #include "TrueBASIC.h" #include "csgraphics.h" int main(); void initial(int x[], int y[], int *N, int *nwalkers, int *im); void move(int x[], int y[], int nwalkers, int im); void choice(int x[], int y[], int i); int main() { int N, nwalkers, step, im; float X1, Y1, X2, Y2; int x[501], y[501]; char Stmp1_[_LBUFF_]; GWopen(0); initial(x, y, &N, &nwalkers, &im); GWsetogn(0, 1); GWgetwn(&X1, &Y1, &X2, &Y2); X1 = X1+(X2-X1)/80; Y1 = Y2-(Y2-Y1)/24; for(step = 1; step <= N; ++step) { GWsetogn(0, -2); sprintf(Stmp1_, "%d", step); GWputtxt(X1, Y1, Stmp1_); move(x, y, nwalkers, im); GWflush(-2); } GWquit(); return 0; } void initial(int x[], int y[], int *N, int *nwalkers, int *im) { int i; double r; float xwin, ywin; char Stmp1_[_LBUFF_]; rnd(-1); printf("number of walkers = "); fgets(Stmp1_, _LBUFF_, stdin); sscanf(Stmp1_, "%d", nwalkers); printf("total number of steps = "); fgets(Stmp1_, _LBUFF_, stdin); sscanf(Stmp1_, "%d", N); compute_aspect_ratio((float)(sqrt((double)(*N)*2)*2), &xwin, &ywin); GWindow(-xwin, -ywin, xwin, ywin); // 原点に N 個の粒子を置く r = 0.5; *im = GWcmbmrk(0, 1, (float)(2*r), RED, -1, -1); for(i = 1; i <= (*nwalkers); ++i) { x[i] = 0; y[i] = 0; } } void move(int x[], int y[], int nwalkers, int im) { int i; for(i = 1; i <= nwalkers; ++i) { choice(x, y, i); GWputcmb(im, (float)(x[i]), (float)(y[i]), 0.0f, 0.0f, 0); } } void choice(int x[], int y[], int i) { double p; p = rnd(0); if(p <= 0.25) ++x[i]; else if(p <= 0.5) --x[i]; else if(p <= 0.75) --y[i]; else ++y[i]; }