randstep2
randstep1のコードを用い、適当な粒子数でシュミレーションを行い、ステップ数を変化させ粒子の位置のヒストグラムを作成する。
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main(void)
{
FILE *one;
one=fopen("result.xls","w");//結果表示の為の物
FILE *two;
two=fopen("date.txt","r");//粒子数を入力
srand( (unsigned)time( NULL ) );
int a;
int po,pos,t,tt,n,nn;
int va[500]={0};//ステップ数が200の時に使う配列
int vb[500]={0};//ステップ数が1000の時に使う配列
int vc[500]={0};//ステップ数が5000の時に使う配列
fscanf(two,"%6ld",&nn);//date.txtから粒子数を読み込む
for (n=0;n<=nn;n++){
pos=250;
for(a=1;a<=3;a++){
switch (a){
case 1:
tt=200;
break;
case 2:
tt=1000;
break;
case 3:
tt=5000;
break;
}//ステップ数の違いによりttの値を変える
for(t=0;t<=tt;t++){
if(rand()<16383){
pos=pos+1;
}
else{
pos=pos-1;
}
}
switch (tt){
case 200:
va[pos]=va[pos]+1;
break;
case 1000:
vb[pos]=vb[pos]+1;
break;
case 5000:
vc[pos]=vc[pos]+1;
break;
}//ttの値の違いにより数値を入れる配列を変える
}
}
for (po=0;po<=499;po++){
fprintf(one,"%3d
",po-250);
fprintf(one,"%6d
",va[po]);
fprintf(one,"%6d
",vb[po]);
fprintf(one,"%6d\n",vc[po]);
}
}