Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "utilfun.h"
- int
- main( int argc, char *argv[] ) {
- int mm, nn;
- int numwon, numlost;
- int tt, ttall, hhall;
- int ii, jj;
- double fract, fractavg;
- seedrand(); // seed random number generator with epoch elapsed time
- mm = 1e6; // number of games to simulate
- nn = 1e4; // number of trials to simulate
- ttall = 0;
- hhall = 0;
- numwon = 0;
- numlost = 0;
- fractavg = 0.0;
- printf("\n");
- printf("%20.0e number of games to play\n", (double) mm);
- printf("%20.0e number of trials per game\n", (double) nn);
- printf("\n");
- printf("Simulating...\n");
- printf("\n");
- fflush(stdout);
- for ( ii = 0; ii < mm; ++ii ) {
- fract = 0.0; // fraction of tails
- tt = 0; // number of tails flipped
- for ( jj = 0; jj < nn; ++jj ) {
- while ( ranf() < 0.5 ) // ranf() -> [0,1) uniform; flip until heads
- ++tt; // while flipping tails
- } // for jj
- ttall += tt;
- hhall += nn; // one head flipped per trial
- fract += (double) tt / (tt + nn); // fraction of tails flipped
- fractavg += fract;
- if ( fract < 0.5 ) ++numwon; // guess stop on heads
- if ( fract > 0.5 ) ++numlost; // guess stop on tails
- } // for ii
- fractavg /= mm;
- printf("%20.12e average fraction T/(T+H)\n", fractavg);
- printf("%20d total number of tails flipped\n", ttall);
- printf("%20d total number of heads flipped\n", hhall);
- printf("%20d number won by strategy guesser\n", numwon);
- printf("%20d number undecided by strategy guesser\n", mm - numwon - numlost);
- printf("%20d number lost by strategy guesser\n", numlost);
- printf("\n");
- return(0);
- } // main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement