Created
December 15, 2013 13:20
-
-
Save tinylamb/7973008 to your computer and use it in GitHub Desktop.
模拟随机游走
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#define START 1 | |
#define END 10 | |
#define TEST 10 | |
#define TIMES 100000 | |
int main(){ | |
srand(time(NULL)); | |
float sum; | |
int count,walk,t,test; | |
for(test=0;test<TEST;test++) { | |
sum=0.0; | |
for(t=0;t<TIMES;t++){ | |
walk=START; | |
count=0; | |
while(walk!=END){ | |
count++; | |
if(rand()%2==0){ | |
if(walk!=START) | |
walk--; | |
} | |
else | |
walk++; | |
} | |
sum+=count; | |
} | |
printf("到达%d的期望时间:%.2f\n",END,sum/TIMES); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment