Docsity

Craps Simulation: Analyzing the Probabilities in Craps Game, Study notes of Computer Science

Toggle sidebar
bg1
CS 1713, Craps Simulation, Thu Mar 05 1998
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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#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%

Partial preview of the text

Download Craps Simulation: Analyzing the Probabilities in Craps Game and more Study notes Computer Science in PDF only on Docsity!

runner% CS 1713, Craps Simulation, Thu Mar 05 1998

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".

{ 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