runner% cat craps.c /* craps.c: gather statistics about playing the * game of craps. Uses time() to initialize the * random number generator. Exact answers * should be 49.28% and 50.71% * In casino craps, for a bet that a loss will occur * (don't pass), an initial roll of 12 still * loses for the pass better, but neither wins * nor loses for the don't pass better. Now * the chance of winning with a bet on "don't * pass" becomes 49.28% also, the same as "pass". * Written by NR Wagner, March 7, 1996 */ #include #include #include #define MAXR 1000000 #define WIN 1 #define LOSE 0 int die(void); int roll(void); int craps(void); void main(void) { int i; int wins = 0, loses = 0; srand48((long)time(NULL)); printf("Total rolls: %1d\n", MAXR); for (i = 0; i < MAXR; i++) { if (craps() == WIN) wins++; else loses++; } printf("Wins:%.4f%%, Loses:%.4f%%\n", 100.0*wins/MAXR, 100.0*loses/MAXR); } /* die: simulate rolling one die */ int die(void) { return (int)(6.0*drand48() + 1.0); } /* roll: simulate rolling two dice */ int roll(void) { return die() + die(); } /* craps: play one game of craps */ int craps(void) { int r; int point; r = roll(); if (r == 2 || r == 3 || r == 12) return LOSE; if (r == 7 || r == 11) return WIN; point = r; for (;;) { r = roll(); if (r == point) return WIN; if (r == 7) return LOSE; } } runner% lint -m -u craps.c function returns value which is always ignored printf runner% cc -o craps craps.c runner% craps Total rolls: 1000000 Wins:49.3299%, Loses:50.6701% runner% craps Total rolls: 1000000 Wins:49.3481%, Loses:50.6519% runner% craps Total rolls: 1000000 Wins:49.2406%, Loses:50.7594% runner% craps Total rolls: 1000000 Wins:49.3314%, Loses:50.6686% runner% craps Total rolls: 50000000 Wins:49.2771%, Loses:50.7229%