
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

cat craps.c
(^) 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".
{ void main(void)int craps(void);int roll(void);int die(void);#define LOSE 0#define WIN 1#define MAXR 1000000#include <time.h>#include <stdlib.h>#include <stdio.h>/ Written by NR Wagner, March 7, 1996 for (i = 0; i < MAXR; i++) {printf("Total rolls: %1d\n", MAXR);srand48((long)time(NULL));int wins = 0, loses = 0;int i; if (craps() == WIN) wins++;
else loses++;
printf("Wins:%.4f%%, Loses:%.4f%%\n",} 100.0wins/MAXR, 100.0loses/MAXR);
{int die(void)/* die: simulate rolling one die /} return (int)(6.0drand48() + 1.0);
{ int roll(void)/* roll: simulate rolling two dice */ return die() + die();
{ int craps(void)/* craps: play one game of craps */} if (r == 2 || r == 3 || r == 12)r = roll();int point;int r; return LOSE;
if (r == 7 || r == 11) return WIN;
for (;;) {point = r; if (r == point)r = roll(); 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
runner%Wins:49.3299%, Loses:50.6701% Total rolls: 1000000 (^) craps
runner%Wins:49.3481%, Loses:50.6519% Total rolls: 1000000 (^) craps
runner%Wins:49.2406%, Loses:50.7594% Total rolls: 1000000 (^) craps
runner%Wins:49.3314%, Loses:50.6686% Total rolls: 1000000 (^) craps
Wins:49.2771%, Loses:50.7229% Total rolls: 50000000