User:Thebigh/mysandbox: Difference between revisions

more
m (blargh)
(more)
Line 6:
 
If you escaped, run 10,000 tests and print the average time taken and the average final length of the staircase.
 
==header|C==
<lang c>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main(void) {
int trial, secs_tot=0, steps_tot=0; //keep track of time and steps over all the trials
int sbeh, slen, wiz, secs; //all the variables related to an individual trial
time_t t;
srand((unsigned) time(&t)); //random number seed
printf( "Seconds steps behind steps ahead\n" );
for( trial=1;trial<=10000;trial++ ) { //10000 attempts for the runner
sbeh = 0; slen = 100; secs = 0; // initialise this trial
while(sbeh<slen) { //as long as the runner is still on the stairs
sbeh+=1; //runner climbs a step
for(wiz=1;wiz<=5;wiz++) { //evil wizard conjures five new steps
if(rand()%slen < sbeh)
sbeh+=1; //maybe a new step is behind us
slen+=1; //either way, the staircase is longer
}
secs+=1; //one second has passed
if(trial==1&&599<secs&&secs<610)
printf("%d %d %d\n", secs, sbeh, slen-sbeh );
}
secs_tot+=secs;
steps_tot+=slen;
}
printf( "Average secs taken: %f\n", secs_tot/10000.0 );
printf( "Average final length of staircase: %f\n", steps_tot/10000.0 );
return 0;
}</lang>
{{out}}<pre>
Seconds steps behind steps ahead
600 2147 953
601 2151 954
602 2156 954
603 2160 955
604 2166 954
605 2169 956
606 2174 956
607 2180 955
608 2184 956
609 2188 957
Average secs taken: 2921.493200
Average final length of staircase: 14707.466000
</pre>
 
==header|Fermat==
781

edits