Skip to content

Instantly share code, notes, and snippets.

@tinylamb
Created December 15, 2013 13:20
Show Gist options
  • Save tinylamb/7973008 to your computer and use it in GitHub Desktop.
Save tinylamb/7973008 to your computer and use it in GitHub Desktop.
模拟随机游走
#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