      PROGRAM box
*     箱の中を運動する粒子のシミュレーション
      IMPLICIT NONE
      INTEGER IR_, N, tmax
      CALL GWopen(IR_, 0)
      CALL initial(N,tmax)
      CALL move(N,tmax)                     ! 粒子が孔を通る
      CALL GWquit(IR_)
      END
C
      SUBROUTINE initial(N,tmax)
      IMPLICIT NONE
      REAL dmy, rnd
      INTEGER IR_, N, tmax
      dmy = rnd(-1)
      WRITE(*,'(A,$)') 'number of particles = '
      READ(*,*) N
      tmax = 20*N
      CALL GWindow(IR_, REAL(-0.01D0*tmax), REAL(-0.01D0*N)
     +, REAL(1.01D0*tmax), REAL(1.01D0*N))
      CALL GWsetpen(IR_, 0, -1, -1, 4)
      CALL GWrect(IR_, 0.0, 0.0, REAL(tmax), REAL(N))
      CALL GWsetpen(IR_, 16, -1, -1, -1)
      CALL GWline(IR_, 0.0, REAL(0.5*N), REAL(tmax), REAL(0.5*N))
                                            ! 平衡状態の値
      END
C
      SUBROUTINE move(N,tmax)
      IMPLICIT NONE
      REAL rnd
      INTEGER IR_, r, nleft, t, N, tmax
      nleft = N                             ! 最初はすべての粒子が左側の部屋にいる
      CALL GWsetpen(IR_, -1, -1, -1, -1)
      CALL GWsetpen(IR_, 13, -1, -1, -1)
      DO t = 1, tmax
*        乱数を生成し粒子を動かす
         r = int(N*rnd(0)) + 1
         IF(r .LE. nleft) THEN
            nleft = nleft - 1
         ELSE
            nleft = nleft + 1
         END IF
         CALL GWline2(IR_, REAL(t), REAL(nleft))
      END DO
      END
