DATA Step, Macro, Functions and more

Craps - Game Simulation

Reply
Trusted Advisor
Posts: 1,300

Craps - Game Simulation

Hi,

If you can't tell from my recent rash of posts I am having a slow week...

Craps is a gambling game:

1. The game is played with 2 dice

2. If the pips on the dice total 2, 3 or 12 the player loses

3. If the pips on the dice total 7 or 11, the player wins

4. If the pips total 4, 5, 6, 8, 9 or 10 the total pips establishes the 'point.'  The player continues to roll until he rolls the point (wins) or rolls a 7 (loses)

Write a program that simulates a game of craps, calculate various statistics from the game:

A. Most Common Roll

B. Win Percentage

C. Number of Rolls/Game

D. Maximum Rolls/Game

E. Whatever else you can think of...

FROM WIKIPEDIA: http://en.wikipedia.org/wiki/Craps

The rules of play against a bank or casino

Bank craps is a game played by multiple players betting against a casino. Each casino might set slightly different payouts for the various bets. Players take turns rolling two dice and whoever is throwing the dice is called the "shooter". Players can bet on the various options by placing chips in the appropriate sections of the board. It may be required to ask the dealer to place certain bets.

While acting as the shooter, a player must have a bet on the "Pass" line or the "Don't Pass" line. Pass and don’t pass are sometimes called "Win" and "Don’t Win" or "Right" and "Wrong" bets. The game is played in rounds and these "Pass" and "Don't Pass" bets are betting on the outcome of a round. The shooter is often replaced at the end of the round or when they lose a round with a seven. The dice are moved clockwise around the table for the next player to become shooter. The shooter is presented with multiple dice (typically five) by the "stickman", and must choose two for the round. The remaining dice are returned to the stickman's bowl and are not used.

Each round has two phases: "come-out" and "point". To start a round, the shooter makes one or more "come-out" rolls. A come-out roll of 2, 3 or 12 loses and is called "craps". Anyone betting on the Pass line loses in this situation. A come-out roll of 7 or 11 (a "natural") wins and results in a payout for "pass line" bets. The other possible numbers are the point numbers: 4, 5, 6, 8, 9, and 10. If the shooter rolls one of these numbers on the come-out roll, this establishes the "point" - the number that must be rolled again before a seven. The dealer flips a button to the "On" side and moves it to the point number signifying the second phase of the round. If the shooter rolls a seven before repeating the point number (a "seven-out"), the Pass line loses and the round ends.

Names of Rolls in Craps

123456
1Snake EyesAce DeuceEasy FourFive (Fever Five)Easy SixNatural or Seven Out
2Ace DeuceHard FourFive (Fever Five)Easy SixNatural or Seven OutEasy Eight
3Easy FourFive (Fever Five)Hard SixNatural or Seven OutEasy EightNine (Nina)
4Five (Fever Five)Easy SixNatural or Seven OutHard EightNine (Nina)Easy Ten
5Easy SixNatural or Seven OutEasy EightNine (Nina)Hard TenYo (Yo-leven)
6Natural or Seven OutEasy EightNine (Nina)Easy TenYo (Yo-leven)Boxcars or Midnight

SPOILER: Below is my blacked-out answer

options fullstimer macrogen symbolgenmlogic mprint;

%let games=500000;

%macro roll;

rolls+1;

%do i=1 %to 2;

roll&i=rand('table',1/6,1/6,1/6,1/6,1/6,1/6);

%end;

pips=sum(of roll1-roll2);

call sortn(roll1,roll2);

if rol.find()=0 then  

do;

  n+1;

  rol.replace();

end;

else

do;

  n=1;

  rol.add();

end;

%mend;

data _null_;

declare hash ga(ordered:'a');

ga.definekey('rolls','i');

ga.definedata('i','rolls','win','loss');

ga.definedone();

declare hash rol();

rol.definekey('roll1','roll2');

rol.definedata('roll1','roll2','n');

rol.definedone();

do i=1 to &games;

rolls=0;

point=.;

%roll

win=ifn(pips in (2,3,12),1,0);

loss=ifn(pips in (7,11),1,0);

if pips in (4,5,6,8,9,10) then

  do;

   point=pips;

     do until(win or loss);

        %roll

        loss=ifn(pips=point,1,0);

        win=ifn(pips=7,1,0);

     end;

       ga.add();

  end;

else ga.add();

end;

declare hiter iga('ga');

declare hiter irol('rol');

max=0;

irol.first();

do while(irol.next()=0);

max=ifn(max<n,n,max);

if max=n then

  do;

   r1=roll1;

      r2=roll2;

  end;

end;

put 'Most Common Roll: ' r1 ' / ' r2 ' with ' n comma8. ' rolls';

tot=0;

wins=0;

winsx=0;

gamesx=0;

iga.last();

put 'Longest game: ' rolls 'rolls';

do while(iga.prev()=0);

tot+rolls;

wins+win;

if rolls>1 then

  do;

   gamesx+1;

   winsx+win;

  end;

end;

avg=tot/&games;

wpct=wins/&games;

wpctx=winsx/gamesx;

put 'Total Rolls Processed: ' tot comma12.;

put 'Average Rolls per Game: ' avg;

put 'Win Percentage: ' wpct percent8.2;

put 'Probability of Winning a Game with More than 1 roll: ' wpctx percent8.2;

run;

SAS Super FREQ
Posts: 3,473

Re: Craps - Game Simulation

Those who have my book Statistical Programming with SAS/IML Software can find a PROC IML solution on pp 322-327. The code is available as a free download from the book's Web page.

Ask a Question
Discussion stats
  • 1 reply
  • 967 views
  • 0 likes
  • 2 in conversation