randstep1

一次元モンテカルロ法のコードを作成し、一粒子でシュミレーションを行い、粒子の位置の時刻歴のグラフを作成する。


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

void main( void )
{
     int a,i;
     a=0;

     FILE *dat;
     dat=fopen("result.xls","w");

     srand( (unsigned)time( NULL ) );

     fprintf(dat,"%6d\n",a);
     for( i = 0; i <= 5000;i++ ){

          if(rand()<16383){
             a=a+1;
             fprintf(dat,"%6d\n",a);
          }
          else{
             a=a-1;
             fprintf(dat,"%6d\n",a);
          }
     }
}